I have a simple script that takes the first argument from the command line and prints hello, first_arg.
But when I pass |ls, it is printing files in the current directory.
My script file
#! /bin/bash
echo "Hello, $1."
I tried multiple things
echo "Hello, '$'"echo "Hello," "$1"printf "Hello %s" "$1"
I want output like Hello, |ls
I checked quoting and this answer https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells
Edit
but It is not happening with input like
echo "input: "
read name
echo "Hello, $name." # output Hello, |ls
echo "Hello, " $name # output Hello, |ls
"|ls"on the command line to prevent expansion (piping tols) by the shell. E.g.bash yourscript "|ls"(single or double quotes are fine here)script '|ls', it will work. If you don't use single or double quotes, then the command line is read as 'output of your script piped tols', andlsdoesn't read its standard input. So the problem is not inside the script — it is all in how you invoke it.lsbefore the script was even started. By quoting the argument you tell the shell to treat it as a string (and argument to your script) and not further shell expressions.