0

I recently encountered a situation where I tried to use ! as a record separator for my string array in shell script. When I used it in my shell script, the script got expanded automatically. An example to reproduce the situation can be as below.

check_variable="Hello"
echo "$check_variable"
Output: Hello
###For multiple strings, I tried to concatenate with ! as a record separator. 
echo "$check_variable !"
Output: echo "$check " > Results.txt

Then after some research, I had figured out ! is in fact a special character and should not be used as record separators. So, exactly why is ! used? Is there any way I can turn off the ! character and use it in my shell script?

Ramesh
  • 39,297
  • If you see the answer, I had added the link that you had specified :) I am not asking a new question here, just trying to elaborate my understanding in a Q & A format :) – Ramesh Mar 25 '14 at 17:56
  • Note: Bash will not expand the ! by default if it is in a shell script. It will do it if you are typing it interactively. Other shells may differ. – Alan Shutko Mar 25 '14 at 19:09

2 Answers2

4

Actually, there is a wonderful explanation on why ! is used from here. To understand the command more clearly, I did the following commands.

pwd- It gives the present working directory.
!pwd - It executes the previously executed pwd command.

This setting could be dangerous as there are chances that an user might execute !rm by mistake and thus resulting in unprecedented results.

Another method to test the effectiveness of the ! character would be,

echo "12345647" >> ramesh.txt
cat ramesh.txt
12345647
!echo
cat ramesh.txt
12345647
12345647

As we can see the !echo command had in fact got executed and appended the line 1234567 to the file.

Though it is not advised to use the special characters as file separators or record separators in the script, this setting could be overrided by executing the below command.

set -H turns it on; 
set +H turns it off.
Ramesh
  • 39,297
0

Not directly related, but keep in mind that enclosing things in "" interpolates said things. In your first line:

check_variable="Hello"

Nothing in your string needs to be interpreted, so why do it? This also goes for when you use $check_variable via echo

check_variable='Hello'
echo $check_variable

This will result in Hello being printed out as you expect.

If you MUST use a reserved character as your separator, don't quote it when using it via echo:

$ echo $check_variable ! > Results.txt
$ cat Results.txt
Hello !
$ echo '$check_variable !'
$check_variable !

Keep in mind that in this case, echo just prints out every argument it receives (the ! in the echo line is considered the second argument) and not all commands will act similarly. Bash doesn't treat it as special in this case because A) it is an argument to another command, and B) you aren't trying to interpolate it by enclosing it in "". The last echo is in there just to show that the use of '' will treat everything inside of it as literal -- no interpolation whatsoever.

Still, I will repeat others and say you should do what you can to avoid using special/reserved characters as anything other than their intended use if at all possible.

BOfH
  • 1