I know that we all love and hate shell scripting. Bash can be the best friend in the world at times, then the worst enemy a second later. While working on a parsing script today, I came across a problem where I wanted to be able to pass an argument to a shell script, then use that argument in some processing. The only problem was, I needed to be able to pass arguments that had multiple words and spaces! I have come across this problem before and worked around it, but today I decided to solve the problem and figure out how to work with it.
In order to pass an argument to a shell script on the command line that is not all one word, you must enclose the command line argument in single quotes and the $1 variable in the code in double quotes. An example:
myEcho.sh
------------------
#!/bin/bash
echo "$1"
Execution:
-----------------
non-root@localhost$> ./myEcho.sh 'hello my loyal blog readers'
hello my loyal blog readers
I hope that this tip helps someone looking for the same thing.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment