Bash-Script: How to insert Variables into Bash-Script?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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?
bash shell-script scripting variable openconnect
add a comment |
up vote
1
down vote
favorite
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?
bash shell-script scripting variable openconnect
1
See what ShellCheck thinks about your script. But also note thatAUTHGROUP=tunnel My Company
is the syntactically correct way to run the commandMy
with the argumentCompany
while first setting the environment variableAUTHGROUP
.
– Kusalananda
13 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
bash shell-script scripting variable openconnect
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?
bash shell-script scripting variable openconnect
bash shell-script scripting variable openconnect
asked 13 hours ago
Dave
347215
347215
1
See what ShellCheck thinks about your script. But also note thatAUTHGROUP=tunnel My Company
is the syntactically correct way to run the commandMy
with the argumentCompany
while first setting the environment variableAUTHGROUP
.
– Kusalananda
13 hours ago
add a comment |
1
See what ShellCheck thinks about your script. But also note thatAUTHGROUP=tunnel My Company
is the syntactically correct way to run the commandMy
with the argumentCompany
while first setting the environment variableAUTHGROUP
.
– Kusalananda
13 hours ago
1
1
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
13 hours ago
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
13 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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 toPASSWD
. There is an issue with the assignment toAUTHGROUP
.
– Kusalananda
13 hours ago
1
@Kusalananda : I’m pretty sure assignment of a string containing ! to a variable without proper quotes wouldn’t work
– Inian
13 hours ago
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 toAUTHGROUP
).
– Kusalananda
13 hours ago
add a comment |
up vote
1
down vote
The same (correct) logic that you are applying when using stings 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.
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.
At the same time, you will need double quotes when using variables: echo '$PASSWD'
will echo the literal $PASSWD
, not the variable's value.
1
@Kusalananda Thanks! I was just editing my answer, but I wouldn't have thought to!
not being interpreted in scripts.
– fra-san
13 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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 toPASSWD
. There is an issue with the assignment toAUTHGROUP
.
– Kusalananda
13 hours ago
1
@Kusalananda : I’m pretty sure assignment of a string containing ! to a variable without proper quotes wouldn’t work
– Inian
13 hours ago
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 toAUTHGROUP
).
– Kusalananda
13 hours ago
add a comment |
up vote
1
down vote
accepted
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 toPASSWD
. There is an issue with the assignment toAUTHGROUP
.
– Kusalananda
13 hours ago
1
@Kusalananda : I’m pretty sure assignment of a string containing ! to a variable without proper quotes wouldn’t work
– Inian
13 hours ago
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 toAUTHGROUP
).
– Kusalananda
13 hours ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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.
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.
edited 13 hours ago
answered 13 hours ago
Inian
3,695823
3,695823
1
There's no issue with the assignment toPASSWD
. There is an issue with the assignment toAUTHGROUP
.
– Kusalananda
13 hours ago
1
@Kusalananda : I’m pretty sure assignment of a string containing ! to a variable without proper quotes wouldn’t work
– Inian
13 hours ago
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 toAUTHGROUP
).
– Kusalananda
13 hours ago
add a comment |
1
There's no issue with the assignment toPASSWD
. There is an issue with the assignment toAUTHGROUP
.
– Kusalananda
13 hours ago
1
@Kusalananda : I’m pretty sure assignment of a string containing ! to a variable without proper quotes wouldn’t work
– Inian
13 hours ago
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 toAUTHGROUP
).
– Kusalananda
13 hours ago
1
1
There's no issue with the assignment to
PASSWD
. There is an issue with the assignment to AUTHGROUP
.– Kusalananda
13 hours ago
There's no issue with the assignment to
PASSWD
. There is an issue with the assignment to AUTHGROUP
.– Kusalananda
13 hours ago
1
1
@Kusalananda : I’m pretty sure assignment of a string containing ! to a variable without proper quotes wouldn’t work
– Inian
13 hours ago
@Kusalananda : I’m pretty sure assignment of a string containing ! to a variable without proper quotes wouldn’t work
– Inian
13 hours ago
2
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
13 hours ago
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
13 hours ago
add a comment |
up vote
1
down vote
The same (correct) logic that you are applying when using stings 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.
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.
At the same time, you will need double quotes when using variables: echo '$PASSWD'
will echo the literal $PASSWD
, not the variable's value.
1
@Kusalananda Thanks! I was just editing my answer, but I wouldn't have thought to!
not being interpreted in scripts.
– fra-san
13 hours ago
add a comment |
up vote
1
down vote
The same (correct) logic that you are applying when using stings 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.
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.
At the same time, you will need double quotes when using variables: echo '$PASSWD'
will echo the literal $PASSWD
, not the variable's value.
1
@Kusalananda Thanks! I was just editing my answer, but I wouldn't have thought to!
not being interpreted in scripts.
– fra-san
13 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
The same (correct) logic that you are applying when using stings 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.
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.
At the same time, you will need double quotes when using variables: echo '$PASSWD'
will echo the literal $PASSWD
, not the variable's value.
The same (correct) logic that you are applying when using stings 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.
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.
At the same time, you will need double quotes when using variables: echo '$PASSWD'
will echo the literal $PASSWD
, not the variable's value.
edited 13 hours ago
answered 13 hours ago
fra-san
43417
43417
1
@Kusalananda Thanks! I was just editing my answer, but I wouldn't have thought to!
not being interpreted in scripts.
– fra-san
13 hours ago
add a comment |
1
@Kusalananda Thanks! I was just editing my answer, but I wouldn't have thought to!
not being interpreted in scripts.
– fra-san
13 hours ago
1
1
@Kusalananda Thanks! I was just editing my answer, but I wouldn't have thought to
!
not being interpreted in scripts.– fra-san
13 hours ago
@Kusalananda Thanks! I was just editing my answer, but I wouldn't have thought to
!
not being interpreted in scripts.– fra-san
13 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481274%2fbash-script-how-to-insert-variables-into-bash-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
See what ShellCheck thinks about your script. But also note that
AUTHGROUP=tunnel My Company
is the syntactically correct way to run the commandMy
with the argumentCompany
while first setting the environment variableAUTHGROUP
.– Kusalananda
13 hours ago