Home > Back-end >  Reassign value of $1 in bash
Reassign value of $1 in bash

Time:12-09

I'm trying to reassign the value of the positional command line argument $1 to another variable to use elsewhere in the script, but it never works. I have put the following code in a new script and found that the value of the new variable is always blank.

dirname = $1
echo "This is \$1:$1"
echo "This is \$dirname:$dirname"

The output of this after running ./test.sh someval is:

This is $1:someval
This is $dirname:

As you can see, 'dirname' is blank, even after being assigned the value of $1. I'm new to bash so likely missing something. Any help is appreciated.

CodePudding user response:

Assignments cannot have whitespace around the =. An assignment is a single word containing a =: the name precedes the (first) =, and the value follows it.

dirname=$1
  •  Tags:  
  • bash
  • Related