How to pass arguments with special characters to call shell script

后端 未结 3 409
有刺的猬
有刺的猬 2020-12-10 20:45

Calling .sh(shell script) with the required parameters as below :-

sh home/example.sh --context_param dbUserName=username --context_param dbPassword=exam!ple         


        
3条回答
  •  眼角桃花
    2020-12-10 21:36

    Change the line,

    dbPassword=exam!ple#####
    

    to,

    dbPassword='exam!ple#####'
    

    to avoid ! (history-expansion) being treated specially in bash

    From man bash under QUOTING sub-section,

    When the command history expansion facilities are being used (see HISTORY EXPANSION below), the history expansion character, usually !, must be quoted to prevent history expansion.

    more under HISTORY EXPANSION

    History expansions are introduced by the appearance of the history expansion character, which is ! by default. Only backslash (\) and single quotes can quote the history expansion character.

    Also, it is a good practice to quote all your name-value pairs to prevent Word-splitting done by shell.

    sh home/example.sh --context_param dbUserName="username" --context_param dbPassword='exam!ple#####' --context_param resultDate="2017-01-13"
    

    About word-splitting, from the man page,

    Word Splitting

    The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words using these characters as field terminators

提交回复
热议问题