How to Pass Arguments with Special Characters to Call Shell Script

How to handle special characters in command line arguments for a shell script

You need to call the script with single quotes around the argument. There is no way to make this work by changing the code of the script.

./testarg.sh '$6$nB3/jwAqgRcS9LN7$8c5InVbk5SLFiwuz9Xm6EHHRuZHMI0ggMhmxJ7Pr7NuBZ0TXtProuZ6LpQ6ZrFySly12cbGmWHsFpukEKAzdW.'

How do I pass arguments containing special characters to a bash script?

Don't try to do shell quoting by hand: Let the shell do it for you.

So, if you have a working local command:

sudo mysql -h BLAHBLAHBLAH.us-east-1.rds.amazonaws.com -u user -p'aaaaa:b>c[d{e]ff=|ggggggggg^$*' adi_chf_db < ./test.sql

...then encapsulate it in a function, by adding a mycmd() { line before and a } line after:

mycmd() {
sudo mysql -h BLAHBLAHBLAH.us-east-1.rds.amazonaws.com -u user -p'aaaaa:b>c[d{e]ff=|ggggggggg^$*' adi_chf_db < ./test.sql
}

...and tell the shell to serialize that function into your ssh session:

ssh test_server "$(declare -f mycmd); mycmd"

Bash: how to pass arguments with special characters like \t

The solution is simple: don't store your command in a variable! See BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!. Also, don't use eval; it causes all sorts of weird problems. If you skip those (and use "$@" properly), the problem vanishes:

my_grep() {
grep "$@"
}

Note that if you call this with my_grep "hello\tworld", it doesn't actually pass in a tab character, it passes a backslash followed by the letter "t" -- which some implementations of grep interpret as matching a tab. If your version of grep doesn't do that, you can pass an actual tab with my_grep $'hello\tworld'.

How to pass variables with special characters into a bash script when called from terminal

Sanitization is absolutely not needed.

The simplest solution, assuming your script is properly executable (has +x permissions and a valid shebang line), is:

./updatelog.sh "$filesize" "$filename"

If for some reason you must use the bash -c, use single quotes instead of double quotes surrounding your code, and keep your data out-of-band from that code:

bash -c 'updatelog.sh "$@"' 'updatelog' "$filesize" "$filename"

Note that only updatelog.sh "$@" is inside the -c argument and parsed as code, and that this string is in single quotes, passed through without any changes whatsoever.

Following it are your arguments $0, $1 and $2; $0 is used when printing error messages, while $1 and $2 go into the list of arguments -- aka $@ -- passed through to updatelog.sh.

pass special characters from input to bash script

The problem is probably not in your script at all, but rather on how you call it. At least from the snippets you provide, it doesn't seem like the password field is being evaluated.

So, when you call the script, if an argument contains something like $a, bash will replace it with the value of the variable a, or an empty string if it is unset.

If $ needs to be in the password, then it needs to be in single quotes.

./adduser_script username 'password$@aaa'


Related Topics



Leave a reply



Submit