bash - Variable substitution in script file and setting a new variable -


i'm attempting run command in shell script , set variable resulting process id. stripped down relevant parts, have:

#!/bin/bash user=myappuser path_to_app=/opt/folder/subfolder pid=`su - $user -c 'nohup $path_to_app/myapp --option > /dev/null 2>&1 & echo $!'`;   echo $pid 

i understand need use double quotes around nohup command variable substitution, if do, pid not being set. if use double quotes , hardcode path_to_app execute , set pid. i'm guessing it's problem combination of tick , single/double quotes.. i'm not sure solution is.

put command in double quotes $path_to_app substituted, , escape $ in $! included literally in argument su, , processed subshell.

pid=$(su - $user -c "nohup $path_to_app/myapp --option > /dev/null 2>&1 & echo \$!") echo $pid 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -