user3116685
user3116685

Reputation: 111

bash set variable from commandline argument

cat ./1.sh

#!/bin/bash
echo $1
set var1 = $1
echo var1 is $var1

kostas@elem:~/1$ argument1 var1 is

How to set var1 from first commandline argument?

Upvotes: 11

Views: 38065

Answers (1)

chepner
chepner

Reputation: 531055

The correct assignment is simply the following, with no spaces on either side of the equal sign:

var1=$1

The command set var1 = $1 actually does the following:

  1. Sets the value of $1 to "var1"
  2. Sets the value of $2 to "="
  3. Sets the value of $3 to the original first parameter $1.

Upvotes: 34

Related Questions