0

The following bash script is working completely fine:

#!/bin/bash

echo '!PaSsWoRd!' | openconnect --csd-wrapper=/home/user/.cisco/csd-wrapper.sh --authenticate --user=abcde123 --authgroup="tunnel My Company" --passwd-on-stdin vpn.mycompany.com

However, I want to replace the previous input parameters with variables like that:

#!/bin/bash

WRAPPER=/home/user/.cisco/csd-wrapper.sh
USER=abcde123
PASSWD=!PaSsWoRd!
AUTHGROUP=tunnel My Company
DOMAIN=vpn.mycompany.com

echo '$PASSWD' | openconnect --csd-wrapper=$WRAPPER --authenticate --user=$USER --authgroup="$AUTHGROUP" --passwd-on-stdin $DOMAIN

Unfortunately this attempt does not work anymore. I think I have to put in some quote chars or similar. Do you know what is wrong with the bash script below?

  • 2
    See what ShellCheck thinks about your script. But also note that AUTHGROUP=tunnel My Company is the syntactically correct way to run the command My with the argument Company while first setting the environment variable AUTHGROUP. – Kusalananda Nov 12 '18 at 15:17
1

The lack of quotes around the assignment to a variable containing ! is the problem here. The shell tries to interpret the ! character to run the history expansion first before assigning it to the variable. The quoting should prevent the shell from interpreting the contents within as '..' as special and keep it as-is.

The assignment should have been written as

PASSWD='!PaSsWoRd!'

and pass the variable with quoted expansion

echo "$PASSWD" | openconnect --csd-wrapper="$WRAPPER" --authenticate --user="$USER" --authgroup="$AUTHGROUP" --passwd-on-stdin "$DOMAIN"

Or turn of the history expansion in the script temporarily by including the line, set +H at the top of the script. Subsequently do set -H at the end to disable it. This is not recommended and much recommended to use the proper quoted approach above.

  • 1
    There's no issue with the assignment to PASSWD. There is an issue with the assignment to AUTHGROUP. – Kusalananda Nov 12 '18 at 15:12
  • 1
    @Kusalananda : I’m pretty sure assignment of a string containing ! to a variable without proper quotes wouldn’t work – Inian Nov 12 '18 at 15:30
  • 2
    There is no issue with this in a non-interactive script since history expansion wouldn't be triggered. There are other good reasons for quoting the password though, as it may contain characters from $IFS, which would make for different problems (the same as in the assignment to AUTHGROUP). – Kusalananda Nov 12 '18 at 15:32
1

The same (correct) logic that you are applying when using strings in your first example has to be used when assigning strings to variables, too.

In AUTHGROUP=tunnel My Company the three words are split. And PASSWD=!PaSsWoRd! works in a script, but bear in mind that the ! would be interpreted in a shell, triggering history expansion.

Note that single quotes will preserve the literal value of each enclosed character, while double quotes will allow for exceptions: $, `, \ and ! (see QUOTING in man bash). Then, PASSWD='!PaSsWoRd!' will work, PASSWD="!PaSsWoRd!" won't (but, again, ! is problematic only in interactive shells, not in scripts).

At the same time, you will need double quotes when using variables: echo '$PASSWD' will echo the literal $PASSWD, not the variable's value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.