简体   繁体   中英

bash echo environment variable containing escaped characters

I have an script that echo the input given, into a file as follows:

echo $@ > file.txt

When I pass a sting like "\\"" I want it to exactly print "\\"" to the file however it prints " . My question is how can I print all characters of a variable containing a string without considering escapes?

When I use echo in bash like echo "\\"" it only prints " while when I use echo '"\\""' it prints it correctly. I thought maybe that would be the solution to use single quotes around the variable, however I cannot get the value of a variable inside single quotes.

First, note that

echo $@ > file.txt

can fail in several ways. Shellcheck identifies one problem (missing quotes on $@ ). See the accepted, and excellent, answer to Why is printf better than echo? for others.

Second, as others have pointed out, there is no practical way for a Bash program to know exactly how parameters were specified on the command line. For instance, for all of these invocations

prog \"
prog "\""
prog '"'

the code in prog will see a $1 value that consists of one double-quote character. Any quoting characters that are used in the invocation of prog are removed by the quote removal part of the shell expansions done by the parent shell process.

Normally that doesn't matter. If variables or parameters contain values that would need to be quoted when entered as literals (eg "\\"" ) they can be used safely, including passing them as parameters to other programs, by quoting uses of the variable or parameter (eg "$1" , "$@" , "$x" ).

There is a problem with variables or parameters that require quoting when entered literally if you need to write them in a way that they can be reused as shell input (eg by using eval or source / . ). Bash supports the %q format specification to the printf builtin to handle this situation. It's not clear what the OP is trying to do, but one possible solution to the question is:

if (( $# > 0 )) ; then
    printf -v quoted_params '%q ' "$@"  # Add all parameters to 'quoted_params'
    printf '%s\n' "${quoted_params% }"  # Remove trailing space when printing
fi >file.txt

That creates an empty 'file.txt' when no positional parameters are provided. The code would need to be changed if that is not what is required.

If you run echo \\" , the function of the backslash in bash is to escape the character after it. This actually enables you to use the double quotes as an argument. You cannot use a backslash by itself; if you want to have a backslash as an argument you need to use another slash to escape that: echo \\\\

Now if you want to create a string where these things are not escaped, use single quotes: echo '\\'

See for a better explanation this post: Difference between single and double quotes in Bash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM