Bash-Script: How to insert Variables into Bash-Script?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












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?










share|improve this question

















  • 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















up vote
1
down vote

favorite
1












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?










share|improve this question

















  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 13 hours ago









Dave

347215




347215







  • 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













  • 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








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











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.






share|improve this answer


















  • 1




    There's no issue with the assignment to PASSWD. There is an issue with the assignment to AUTHGROUP.
    – 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 to AUTHGROUP).
    – Kusalananda
    13 hours ago


















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.






share|improve this answer


















  • 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










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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.






share|improve this answer


















  • 1




    There's no issue with the assignment to PASSWD. There is an issue with the assignment to AUTHGROUP.
    – 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 to AUTHGROUP).
    – Kusalananda
    13 hours ago















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.






share|improve this answer


















  • 1




    There's no issue with the assignment to PASSWD. There is an issue with the assignment to AUTHGROUP.
    – 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 to AUTHGROUP).
    – Kusalananda
    13 hours ago













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited 13 hours ago

























answered 13 hours ago









Inian

3,695823




3,695823







  • 1




    There's no issue with the assignment to PASSWD. There is an issue with the assignment to AUTHGROUP.
    – 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 to AUTHGROUP).
    – Kusalananda
    13 hours ago













  • 1




    There's no issue with the assignment to PASSWD. There is an issue with the assignment to AUTHGROUP.
    – 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 to AUTHGROUP).
    – 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













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.






share|improve this answer


















  • 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














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.






share|improve this answer


















  • 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












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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












  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay