I try to pass a string containted between two #
sign to a ksh script :
Will #> ./a.ksh #This is a string#
Will #>
Nothing is prompted, the execpeted output would be :
Will #> ./a.ksh #This is a string#
#This is a string#
Below the a.ksh
script.
a.ksh
echo $1
echo "$1"
echo ${1}
echo "${1}"
I have tried to protect my
$1
variable by any means I knew but I can't get anything starting with#
to be displayed.
Those following don't work :
#> ./a.ksh #Hello
#> ./a.ksh #
But this do :
#> ./a.ksh Hello#
Any shell guru that could explain me why and how to get this to work as excpeted ?
I can escape those string using \#This String#
or "#This String#"
but I wonder why does #
doesn't print by itself.
#
is interpreted as the start of a comment. All comments are ignored by the interpreter.You have two options:
Quote the string to make it a literal:
./a.ksh "#This is a string#"
Have your script read input from the user instead of a parameter:
Here's an example:
then run the program first and enter your data after: