I want to store the second line of my file into a variable, so I am doing this:
sed -n '2p' myfile
I wish to store the output of the sed command into a variable named line.
What is the correct syntax to do this?
I want to store the second line of my file into a variable, so I am doing this:
sed -n '2p' myfile
I wish to store the output of the sed command into a variable named line.
What is the correct syntax to do this?
 
    
    Use command substitution like this:
line=$(sed -n '2p' myfile)
echo "$line"
Also note that there is no space around the = sign.
 
    
    In general,
variable=$(command)
or
variable=`command`
The latter one is the old syntax, prefer $(command).
Note: variable = .... means execute the command variable with the first argument =, the second ....
 
    
    