This question already has answers here:
here document and double backslash
(2 answers)
Closed 8 years ago.
I am trying to pass some code to a REPL and this heredoc seems to be escaping my regex. To shorten the problem... I have a really long regex but this is the main problem:
<<SOMECODE
\\d
SOMECODE
This is returned by the heredoc as
\d
How do I get the heredoc to not remove my extra slash? I thought heredocs were immune to characters.
Quote Your Here-Document Delimiter
If you want to prevent most escapes and expansions, you can surround your Bash here-document delimiter with single quotes. For example:
cat << 'SOMECODE'
\\d
SOMECODE
prints \\d on my system.
Related
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
USER_UID=$1
echo 'generate_token("$USER_UID")'
I want output like
generate_token("1234567")
i tried multiple ways but didn't worked. it just print same line without value generate_jwt("$USER_UID")
When you use single quotes, it causes the shell to preserve the literal value of each character within the quotes. This means the $ will be treated as a literal $ character.
You should use double quotes:
USER_UID="$1"
echo "generate_token(\"$USER_UID\")"
From the bash man page, under the Quoting section:
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.
For POSIX details on quoting, see here.
Example in an interactive shell:
$ USER_UID='foo'
$ echo "generate_token(\"$USER_UID\")"
generate_token("foo")
This will also work if USER_UID contains spaces:
$ USER_UID='var with spaces'
$ echo "generate_token(\"$USER_UID\")"
generate_token("var with spaces")
This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Closed 3 years ago.
I need to replace the special character which is not alphanumeric with a backslash in a string.
How do i do it in Bash? My version is 4.1
I can capture the special character the plus symbol using the following regex
([^[:alnum:]])
For example, applied to the string
Alan5+6imson
I can do
$ echo $orig_str |sed 's/([^[:alnum:]])/\\1/g'
Alan5+6imson
I need the output as
Alan5\+6imson
How can I replace it in Bash?
I tried the above regex but not sure how to perform a replacement.
Do i need to use some other tool or something like sed?
Would you please try:
echo "$orig_str" | sed 's/\([^[:alnum:]]\)/\\\1/g'
or:
echo "$orig_str" | sed 's/[^[:alnum:]]/\\&/g'
This question already has answers here:
What is the most aesthetic way to to escape a single quote within a single-quoted string in (ba)sh?
(5 answers)
How to escape single quotes within single quoted strings
(25 answers)
single quote escaping in bash within single quoted string
(3 answers)
Closed 3 years ago.
Trying to use Linux bash script to write to a file but unable to write the below line because of the extra
" and ' symbols in the line. Is there a better way to write a file with linux bash script or a way that will work?
echo "add_header Content-Security-Policy "default-src 'self' https://*.jsdelivr.n$ >> $file
I'm a noob to bash scripting and appreciate the help.
There are several ways to do this. A heredoc is a good choice:
cat >> $file << \EOF
add_header Content-Security-Policy "default-src 'self' https://*.jsdelivr.n$
EOF
This simply takes everything until the EOF verbatim as input to cat, and writes it to the file. (It's not clear to me if you want a double quote before add_header; if so, prepend it.) Another reasonable alternative is to escape double quotes in a double quoted string:
echo "add_header Content-Security-Policy \"default-src 'self' https://*.jsdelivr.n$" >> $file
This question already has answers here:
How does "cat << EOF" work in bash?
(12 answers)
Closed 6 years ago.
In this question and in this question this kind of BASH script syntax is used:
#!/bin/bash
sql << EOF
**some SQL commands**
EOF
Where EOF can in fact be anything. It works for me, but I am curious why it works and how. What is the right way to call this pattern. And can you find the Bash documentation corresponding to this feature?
% man bash | grep --max-count=1 '<<' -C 7
Here Documents
This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks)
is seen. All of the lines read up to that point are then used as the standard input for a command.
The format of here-documents is:
<<[-]word
here-document
delimiter
No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in
word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted,
all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \<new‐
line> is ignored, and \ must be used to quote the characters \, $, and `.
The construct you've found is called a "here document" and is described in the Bash manual under the heading "Here Documents" (followed by "Here Strings" that are related). It is a standard feature of any shell implementing the POSIX standard for shells. I believe that "here strings" is a Bash extension though.
It is good practice to do as in your example and use an upper-case string to mark the ending of the here document. Another good suggestions is to use XXX_END (with XXX being, e.g., XML, DOC, DATA, SQL or whatever it is you're feeding into the command) as the delimiter to further document the here document.
If the word used as a delimiter is quoted, the shell will not do parameter expansion (etc.) on the contents of the here document:
$ cat <<'TEST_END'
my $HOME is where I hang my hat
TEST_END
results in:
my $HOME is where I hang my hat
while
$ cat <<TEST_END
my $HOME is where I hang my hat
TEST_END
(on my laptop) results in
my /Users/kk is where I hang my hat
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Shell Programming: What's the difference between $(command) and command
It seems there is no difference between
$()
and
``
For example,
$(date)
is the same as
`date`
Any suggestion ?
What's the difference between man command and stackoverflow.com? :)
When the old-style backquote form of substitution is used, backslash
retains its literal meaning except when followed by $, `, or \.
The first backquote not preceded by a backslash terminates the command
substitution. When using the $(command) form, all characters between
the parentheses make up the command; none are treated specially.
so, if you put date inside, it's more or less the same.