98521
98521

Reputation: 3

How to call a variable as a path from another script?

Example

Var = '/etc/sysconfig/..'
export Var
bash script1.sh

in another script1

cat $Var

This is my Problem: The variable does not call the file in this path

Upvotes: 0

Views: 95

Answers (3)

aze2201
aze2201

Reputation: 559

I think no need to to more thing

script 1

#!/bin/bash
a="/home/example"  ### you can do with export command also export a="/home/example"

sctipt2 ## make effective

. script1; 
cd $a

Upvotes: 0

Andreas Louv
Andreas Louv

Reputation: 47127

Your variable assignment is wrong, it should be:

Var='/etc/sysconfig/..' 

No spaces around =.

If you want to send in a environment variable for one script only then you can use:

Var='/etc/sysconfig/..' ./my_script.sh

And inside my_script.sh:

printf "%s\n" "$Var"
# Will print /etc/sysconfig/..

If you want to send arguments to my_script.sh do what @JohnZwinck suggested. What I suggested is only to change environment variable and shouldn't be abused to send/receive regular variables to a command.

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249642

Do this:

Var='/etc/sysconfig/..'
bash script1.sh "$Var"

Then in script1.sh:

Var=$1
cat "$Var"

The quotes around "$Var" are required to support paths containing spaces.

Upvotes: 2

Related Questions