shell interprets quotes as string - bash

I'm new to shell script, so one of the first things to learn is something like:
echo "Hello there"
However, this returns:
"Hello there"
The shell seems to interpret quotes as part of the string.
This is not limited to the echo command, but also, for example: printf or read.

These are not equivalent: ¨ and ". In the shell, use " (34, \x22) for double quotes, not “ or ” etc.

Related

BASH - Escape three backslash

I want to display three backslashes in bash. I tried the codes below:
myString='\\\hello world'
echo ${myString} # output is '\\hello world'
I'm planning to put all kinds of special characters in the string. My question is how to treat it like a string and not escape any characters. Thanks!
Your code works in bash, but you're probably incorrectly running it with sh. In any case, use printf to avoid any unexpected expansions:
myString='\\\hello world'
printf '%s\n' "${myString}"
Try the following:
#!/bin/bash
test="\\\\\\hello world"
echo $test
You need to mask each special char (backslash) with an backslash. You need 3 special chars, so you need 3 backslashs.

Single and double quotes in the same bash input

I want to give input to a bash read function which contains both single and double quotes at the same time, then have it compare with a stored value.
This script asks a random question on startup, looking for correct syntax of some commands I want to memorize. However some of these commands contain both single and double quotes, so how to handle this with bash?
#!/bin/bash
echo "What is the secret string?"
read secret
if [ "$secret" = "123" ]
then
echo "You're Awesome!!!"
else
echo "You're memory sucks!!"
fi
So if $question is "Hi my name is 'Ed'" including the single AND double quotes in the same input, how do I work this magic?
There is no magic to be worked. The input from the user never becomes syntax within your script — it is never evaluated as code — so it can contain anything.*
* Except for the NUL character (\0) itself, since that is the string terminator character.
reading strings that contain quotes doesn't require anything special, but I'd use read -r to prevent it messing with backslashes. You can also use the -p option to supply a prompt (instead of echoing it separately):
read -r -p "What is the secret string? " secret
The only tricky thing here is specifying the string to compare with. One option is to express it as a double-quoted string, and escape the double-quote(s) within it with backslashes. You'll also need to escape any dollar signs, backticks, or backslashes the same way:
if [ "$secret" = "dq: \", sq: ', dollar: \$" ]
then
echo "You're Awesome!!!"
else
echo "Your memory sucks!!"
fi
Here's an example run of the above code:
$ ./secrettest.sh
What is the secret string? dq: ", sq: ', dollar: $
You're Awesome!!!
My question was about specifying the string in the code as Gordon mentioned, not reading it from the input. I had escaped the double quotes and single quotes in my original which is why it didnt work.
So this
if [ "$secret" = "command \'with \"options\"\'" ]
Should have been this
if [ "$secret" = "command 'with \"options\"'" ]
Thanks guys.

I want to print $ in shell, but it is always deleted

In a Linux shell, I want to print:
$300
$400
But when I do echo -e "$300\n$400" it shows:
00
00
When I do printf "$300\n$400" it shows the same thing!
So why does shell delete my dollar sign and the number right after it? Is there a way to print what I want?
You need to escape dollar $, since you are using double quotes, This will ensure the word is not interpreted by the shell.
$ echo -e "\$300\n\$400"
$300
$400
You may be aware how to access variables,
Example :
$ test="foo"
$ echo "$test"
foo
Suppose if you want to print $test, then you have use either
$ echo "\$test"
$test
OR with single quotes
$ echo '$test'
$test
In the shell, the $ character has a special meaning. It means "replace the $ and the following word or digit or special character with the value of a variable of that name". For example:
currency='EUR'
echo "The currency is $currency"
The variables 0, 1, 2, etc. contain the command line arguments to the program. So if you run your program as my-program Hello, world, you can write this code:
echo "argument 1 is $1"
echo "argument 2 is $2"
echo "both together are $1 $2, and all arguments are $*"
To make the $ character lose this special meaning, it must be written as \$. For example:
price=123
echo "The price is $price\$"
The first $ refers to the variable, and the second $ is escaped.
Alternatively you can surround your string in 'single quotes', which removes the special meaning of all characters.
To learn more about this topic, run the man bash command and read the section about variable expansion.
$ has special meaning to the shell; when it sees a $, it expects an existing shell variable name to follow. For example, $PATH.
In your case, you don't want the shell to think that you're trying to print out the value of shell variables, so you must tell the shell that the $ is indeed what you want to be displayed. This is done by preceding it with a backslash as explained in other answers.
Adding a backslash before characters is called escaping them (yes, not the most obvious terminology), and you are already using some escape characters unknowingly. (\n)
This applies to display other operators too, such as =, :, etc. Hope that helps.
You can use single quote. Enclosing characters in single-quotes (') shall preserve the literal value of each character within the single-quotes, where as enclosing characters in double-quotes(") shall preserve the literal value of all characters within the double-quotes, with the exception of the characters back quote, dollar-sign, and backslash.
echo -e '$'300"\n"'$'400

How to have both semicolons and variables in a URL with cURL?

My question is how can I make this bash code run:
#!/bin/sh
actionString = printpage
curl 'www.example.com/index.php?action=$actionString;post=5'
My problem is that if I do not escape the URL with quotation marks, then it will stop processing the URL after the ";", however if I do have it in quotation marks it won't recognize the variable. Is there some trick to getting past this? Thanks for the help.
Use the quotes that don't inhibit parameter substitution.
curl "http://www.example.com/index.php?action=$actionString;post=5"
Variables are NOT evaluated within single quotes:
greeting=hello
echo 'say $greeting; ...' # outputs literally: say $greeting; ...
Variables ARE evaluated within double quotes:
echo "say $greeting" # outputs: say hello; ...
You can quote only the part of parameters that needs quoting, and you can use double quotes for some parts and single quotes for others. All of these are equivalent:
echo say $greeting"; ..."
echo say $greeting'; ...'
echo "say $greeting"'; ...'
echo say $greeting\; ...
So in your case, these would all work:
curl "http://www.example.com/index.php?action=$actionString;post=5"
curl http://www.example.com/index.php?action="$actionString;post=5"
curl http://www.example.com/index.php?action="$actionString;"post=5
and if actionString doesn't have special characters, this would work too:
curl http://www.example.com/index.php?action=$actionString';post=5'

Bash function, return bold text

I'm trying to write a simple bash function that returns bold text. The code I have written so far is:
function txt_bold() {<br>
echo -e '\033[1m$1\033[0m$2'<br>
tput sgr0<br>
}
When I write txt_bold "This is bold" "And this in plain text" it returns "$1$2" ($1 in bold). What am I doing wrong here?
Use " instead of '.
function txt_bold() {
echo -e "\033[1m$1\033[0m$2"
tput sgr0
}
Short
Within single quotes variables are not getting expanded.
Long
Below's the bottom line of this article, which might help you understand it: What’s the Difference Between Single and Double Quotes in the Bash Shell?
Double Quotes
Use when you want to enclose variables or use shell expansion inside a string.
All characters within are interpreted as regular characters except for $ or ` which will be expanded on the shell.
Single Quotes
All characters within single quotes are interpreted as a string character.

Resources