Search code examples
sedsudosu

Why could not get any output when use command sudo su -c "sed -n -e '$p' a.txt"


In Centos 6.7 Linux,I can get one line of output if I run the command:

sed -n -e '$p' a.txt

enter image description here

But if I use this command, it has no output:

sudo su -c "sed -n -e '$p' a.txt"

enter image description here


Solution

  • In the first command: sed -n -e '$p' a.txt. The '$p', the variable substitution is quoted in single quote '$p', then it will contain it's literal meaning, that is literal $p, no expansion.

    In the second commmand: sudo su -c "sed -n -e '$p' a.txt". The $p is included in double quote, though it is included in single quote '', but the single quote is just character in double quote. It has no effect on the variable substitution of $p. So the variable substitution will be expanded.

    For example:

    p="hello"
    echo '$p'    # print $p
    echo "'$p'"  # print 'hello'