Reputation: 305
I have an environment variable
net_connect="@oradb"
So I am trying to store that value in another environment variable without the @ character
So doing this seems to work:
echo "test=" $net_connect | cut -d "@" -f2
test=oradb
Now if I do this I get:
SID=`$net_connect | cut -d "@" -f2`
echo "SID=" $SID
SID=
Why is my output blank?
Upvotes: 1
Views: 57
Reputation: 113864
To remove the @
, use prefix removal:
$ net_connect="@oradb"
$ sid="${net_connect#@}"
$ echo "$sid"
oradb
Notes:
In general, ${variable#prefix}
removes the glob prefix
from variable
. For more information, open man bash
and search on Remove matching prefix pattern
In shell, it is best practices to name your variables with mixed or lower case. If you use upper case variable names, then eventually you will, by accident, overwrite one of the system's variables and the results will be unpleasant.
The following command does not do what you hope:
SID=`$net_connect | cut -d "@" -f2`
It runs the command $net_connect
and sends its stdout to cut
. But, there is likely no such command and the shell will issue an error such as:
bash: @oradb: command not found
If you really wanted to use cut
, then:
sid=$(echo "$net_connect" | cut -d "@" -f2) # This works but prefix removal is simpler
Upvotes: 4
Reputation: 360702
Because you're executing the variable, not echoing it:
$net_connect | etc...
boils down to
@oradb | etc...
which is a syntax error. You have no command/binary named @oradb
.
Maybe you wanted
SID=`echo $net_connect | cut -d "@" -f2`
instead.
Upvotes: 2