I tried printing "Dogs are the best." with this bash script.
#!/bin/bash ANIMAL="Dog" echo "$ANIMALs are the best." exit
However, I got " are the best." printed out instead because the s
in $ANIMALS
is not separated from the variable. How do I separate it?
In a shell, the most common way to escape special characters is to use a backslash before the characters. These special characters include characters like ?, +, $, !, and [. The other characters like ?, !, and $ have special meaning in the shell as well.
-S filename ] can be read as "not is-socket filename". So the command is checking whether a "socket" (a special kind of file) exists with each name in the loop. The script uses this command as the argument to an if statement (which can take any command, not just [ ) and sets DOWN to true if any of them does not exist.
With braces: echo "${ANIMAL}s are the best."
With quotes: echo "$ANIMAL"'s are the best.'
With printf: printf '%ss are the best.\n' "$ANIMAL"
I wouldn't use the quotes one most of the time. I don't find it readable, but it's good to be aware of.
Just surround the variable's name with curly braces.
#!/bin/bash ANIMAL="Dog" echo "${ANIMAL}s are the best." exit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With