Search code examples
bashshellunixio

How do I retrieve exact input to bash from command line?


How do I get bash to read input from the command line literally? I want the exact input to be preserved, so if there are quotes I want to keep them. I can accomplish what I want to do by using egrep to read from file and then sanitizing the input, like so:

egrep '/.*/' filename |
sed 's/\(.*\)['"'"']\(.*\) \(.*\)['"'"']\(.*\)/\1'"\'"'\2" "\3'"\'"'\4/g'

with "filename" containing this line

sed 's/foo/foo bar/g' file

this gives me the desired output of:

sed 's/foo/foo" "bar/g' file

Problem here is that I can't echo "$@" because bash interprets the quotes. I want the literal input without having to read from file.


Solution

  • You could use ANSI-C quoting:

    bash filename $'sed \'s/foo/foo" "bar/g\' file'
    

    Note that you'd need to escape the single quotes. This would produce:

    sed 's/foo/foo" "bar/g' file
    

    upon saying

    echo "$@"