bdf7kt
bdf7kt

Reputation: 162

How do I avoid calling part of my string as a command?

I run with the file with command line arguments: samplebash.bsh fakeusername fakepassword&123

.bsh file:

echo "Beginning script..."
argUsername='$1'
argPassword='$2'
protractor indv.js --params.login.username=$argUsername --params.login.password=$argPassword

Output:

Beginning script...

123: command not found

The Issue: For some reason, it interprets what follows the & symbol from the password as a command, how do I avoid this?

Upvotes: 0

Views: 126

Answers (1)

Barmar
Barmar

Reputation: 781741

The problem isn't happening in your script, it's happening in your original command line. & is a command terminator, which specifies that the command before it should be executed in the background. So your command was equivalent to:

samplebash.bsh fakeusername fakepassword &
123

You need to quote the argument to prevent special characters from being interpreted by the shell.

samplebash.bsh fakeusername 'fakepassword&123'

Also, you shouldn't put single quotes around a variable like you do in your assignments, that prevents the variable from being expanded. So it should be:

argUsername=$1
argPassword=$2

And you should put double quotes around the variables when you use them in the command, to prevent wildcards and whitespace from being interpreted.

protractor indv.js --params.login.username="$argUsername" --params.login.password="$argPassword"

As a general rule, you should always put double quotes around variables unless you know they're not needed.

Upvotes: 7

Related Questions