-3

I'm trying to execute below bash shell script, but not getting the expected output. Possible i'm doing something wrong or it's not the way of doing this.

 #bin/bash
#set -x

path1_one=/home/dell/scripts
echo $path1_one
param_val=path1_one
param1=$( echo "$param_val" | awk -F '_' '{ print $0 }' )

#path2="$path1"
echo $param1

#echo $path2

Output:

/home/dell/scripts

path1_one

Expected Output:

/home/dell/scripts

/home/dell/scripts

Both variable value should be same,but don't know why param1 value is not reflecting with path1_one

subodh
  • 6,136
  • 12
  • 51
  • 73
  • 1
    Use `path2="$path1"` – NullDev Mar 24 '17 at 10:51
  • It isn't obvious why you think your "expected" output is more correct than your actual output. `param_val=path1_one` assigns the *string* `path1_one` to `param_val`; why would anything "reflect"? – Charles Duffy Mar 24 '17 at 16:02
  • ...if you **want** indirect assignment or indirect expansion, we already have questions unambiguously asking how to do those things and with clear and readable answers. – Charles Duffy Mar 24 '17 at 16:04
  • @CharlesDuffy, param1=path1_one, so it should print the path1_one value which is /home/dell/scripts – subodh Mar 24 '17 at 16:11
  • use `param_val="$path1_one"` instead of `param_val=path1_one`. as everybody told you. they just used different variables to demonstrate that and expected you to adapt it to your script. – Samuel Kirschner Mar 24 '17 at 16:20
  • @subodh, no, `param1=path1_one` means that the **string** `path1_one` is stored in the variable `param1`. It **does not** mean that your variables have identical values or are references to each other. – Charles Duffy Mar 24 '17 at 16:24
  • @subodh, ...so, again: What do you expect or want to happen? If you want to look up the value of a variable named in a string, for instance, there **is** syntax to do so, and I'd be happy to mark this as duplicate of a question asking for that syntax. By contrast, if you want to establish a runtime linking -- ie. a "namevar" -- there's syntax for that too. But right now, it isn't established what mechanic you're aiming for. – Charles Duffy Mar 24 '17 at 16:25
  • @subodh, ...so, here's a question that'll help make the distinction. If after you've run `param_val=path1_one`, if someone then runs `param_val=two`, do you expect the value of the variable named `path1_one` to be `two`? If so, you want a namevar. If not, you want an indirect expansion. – Charles Duffy Mar 24 '17 at 16:27
  • @CharlesDuffy, thanks for your quick response,i think i need indirect expansion, you can mark this as duplicate with other question – subodh Mar 24 '17 at 16:30

1 Answers1

3

You need to tell the script that you want to use the value of the variable path1, not the name path1.

Use:

path2="$path1" 
chepner
  • 497,756
  • 71
  • 530
  • 681
NullDev
  • 6,739
  • 4
  • 30
  • 54