Bash Echo with an $ Character Outside the String

Why can't I echo the characters #! in a string in bash?

This is not a bug and it has nothing to do with the shebang, just the exclamation point.

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, !.

So either escape it, use single quotes, or turn off history expansion.

e.g.

> echo "How dare you put an '!' in this string?"
bash: !: event not found
> set +o histexpand
> echo "How dare you put an '!' in this string?"
How dare you put an '!' in this string?

How to echo an * character to console in shell

Or, "thinking outside the box",

just use a different char? # ?

IHTH

Extract substring in Bash

Use cut:

echo 'someletters_12345_moreleters.ext' | cut -d'_' -f 2

More generic:

INPUT='someletters_12345_moreleters.ext'
SUBSTRING=$(echo $INPUT| cut -d'_' -f 2)
echo $SUBSTRING

Echo newline in Bash prints literal \n

Use printf instead:

printf "hello\nworld\n"

printf behaves more consistently across different environments than echo.

How to check if a string contains a substring in Bash

You can use Marcus's answer (* wildcards) outside a case statement, too, if you use double brackets:

string='My long string'
if [[ $string == *"My long"* ]]; then
echo "It's there!"
fi

Note that spaces in the needle string need to be placed between double quotes, and the * wildcards should be outside. Also note that a simple comparison operator is used (i.e. ==), not the regex operator =~.

echo -n prints -n

There are multiple versions of the echo command, with different behaviors. Apparently the shell used for your script uses a version that doesn't recognize -n.

The printf command has much more consistent behavior. echo is fine for simple things like echo hello, but I suggest using printf for anything more complicated.

What system are you on, and what shell does your script use?

How are backslashs interpreted outside quotation?

Outside of quotes, unescaped backslashes are always deleted. They are only used to disable the special meaning of other symbols.

Inside double quotes, backslashes are kept, except when escaping one of $, `, ", \, or marking a line continuation.

From POSIX Shell Command Language, emphasis mine:

2.2.1 Escape Character (Backslash)

A <backslash> that is not quoted shall preserve the literal value of the following character, with the exception of a <newline>. If a <newline> follows the <backslash>, the shell shall interpret this as line continuation. The <backslash> and <newline> shall be removed before splitting the input into tokens. [....]

2.2.3 Double-Quotes

Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the exception of the characters backquote, <dollar-sign>, and <backslash>, as follows:

[...]

\

The <backslash> shall retain its special meaning as an escape character (see Escape Character (Backslash)) only when followed by one of the following characters when considered special:

$ ` " \ <newline>

Replace a string in shell script by a string with special character

You need to use another sed command to add a backslash before all the special characters in the given input string.

$ name="test&commit"
$ name1=$(sed 's/[^[:alpha:][:digit:][:blank:]]/\\&/g' <<<"$name")
$ echo $name1
test\&commit
$ echo "{{name}}" | sed "s/{{name}}/$name1/g"
test&commit

It would be minimized as,

$ name="test&commit"
$ echo "{{name}}" | sed "s/{{name}}/$(sed 's/[^[:alpha:][:digit:][:blank:]]/\\&/g' <<<"$name")/g"
test&commit


Related Topics



Leave a reply



Submit