Ruggero Turra
Ruggero Turra

Reputation: 17670

strange error when passing argumento to bash script

I am not expert of bash scripting, but I really don't understand what is appening here. My script is this:

#!/usr/bin/env bash

echo "calling asetup"
export ATLAS_LOCAL_ROOT_BASE=/cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase
source ${ATLAS_LOCAL_ROOT_BASE}/user/atlasLocalSetup.sh
asetup 17.6.0,slc5

echo "Now running..."
echo "argument $@"

I call it with as ./myscript -v, the output is:

calling asetup
atlasLocalSetup.sh: invalid option -- 'v'
'atlasLocalSetup.sh --help' for more information
./prova.sh: line 12: asetup: command not found
Now running...
argument -v

on the second line, what is atlasLocalSetup.sh: invalid option -- 'v'?? Why isatlasLocalSetup.sh called with the -v option?

Upvotes: 1

Views: 101

Answers (2)

choroba
choroba

Reputation: 241788

The sourced script is run in the current environment without changing the value of positional parameters. The value of "$@" is the same for the called script as for the calling script.

You can use

set --

to clear the positional parameters. If you need to save them, use

pos_par=("$@")
set --
source script.sh
set -- "${pos_par[@]}"

Upvotes: 3

rici
rici

Reputation: 241681

It seems like you are calling your script file with a -v option. Note that source does not change the values of the commandline parameters ($1) unless positional parameters are provided to the source command.

Upvotes: 0

Related Questions