This is kind of a weird one. I have the following string:
I have a variable called REDIRECT set to: https://working.${MYDOMAIN}/blah/blah.
I need to replace the ${MYDOMAIN} with the actual value of the variable assigned to ${MYDOMAIN}.
Not sure if bash or sed is best for this. I tried bash replace but couldn't get it to work, probably related to escaping the characters or something. Any help appreciated!
You may use this bash substitution:
echo "${REDIRECT/\${MYDOMAIN\}/$MYDOMAIN}"
or else, if you have envsubst utility then use:
export MYDOMAIN
envsubst <<< "$REDIRECT"
Just execute in bash:
eval REDIRECT=$REDIRECT
Related
For example, I have below in a script. I expect the output from echo to be 5, but I get demo. Can someone help me with the correct syntax?
C_id_demo=5
var1="demo"
echo $C_id_${var1}
You can use eval for this:
eval echo \$C_id_${var1}
Note 1: the \ is there for the full variable is evaluated by the echo, not the eval.
Note 2: usage of eval may be dangerous. Be sure that you really know what is in the evaluated variable.
I am confused by how to use variables in Bash. Please see the following example. I am not able to figure out why Bash isn't able to recognize the variable within (). Can anybody please help me understand what is going on.
$echo $SHELL
/bin/bash
$export TestC=/Users
$echo $TestC
/Users
$export TestD=$TestC/ABCD
$echo $TestD
/Users/ABCD
$export TestD=$(TestC)/ABCD
-bash: TestC: command not found
Thanks for your help
When referencing a bash variable you either use $ then the name, as in $TestC or you can put braces around the name like ${TestC}.
$(...) is a subshell syntax called command substitution that will execute the command inside the parens then "return" the stdout of that command.
Read all about parameter/variable expansion here, which also shows a lot of the extra things you can do with the parameter expansion when using braces.
I have a shell script that need to use a variable following a dash to generate the file names.Here is what I did:
var="5"
echo "filename$var_something.txt"
However, it always output error, because the script treats $var_something as a variable.
How can I solve this?
Thanks.
echo "filename${var}_something.txt" use braces to delimit your variable name.
Why does the following work from the prompt but fail when stuck inside a bash script? The bash script produces one empty line leading me to believe the variable isn't being set:
echo "red sox" | read my_var
echo $my_var
UPDATE: Since I guess my example isn't working, what I'm really trying to do is take I/O and pipe it into a variable to so I can do things with it. How do I do this? I thought this should be taken care of by the read command, but maybe there's another way?
If you are asking this because you simplified from a more general problem such as:
someprog args | read my_var
you should be using command substitution:
my_var=$(someprog args)
The reason read doesn't work in a pipe the way you have it is that it creates a subshell. Variables set in a subshell don't persist to their parents. See BashFAQ/024.
You can store the output of a command in a variable using either backticks or $() syntax:
my_var=`cat /some/file.txt`
Now the content of /some/file.txt is stored in $my_var. Same thing, with different syntax:
my_var=$(cat /some/file.txt)
It doesn't work at the prompt. My guess is you already have my_var set in your CLI, and are just retrieving that at the prompt.
Try this:
$ my_var="nothing"; echo "red sox" | read my_var; echo $my_var
If you want the my_var variable to have a constant value, as in your question, why not just do:
my_var="red sox"
What are you trying to do? Please explain what you wan't to do first.
If you're trying to set a variable this is what you need:
my_var="red sox"
echo $my_var
If you need it globally you should set it in env:
export my_var
i have unix shell script which is need to be run like below
test_sh XYZ=KLMN
the content of the script is
#!/bin/ksh
echo $XYZ
for using the value of XYZ i have do set -k before i run the script.
is there a way where i can do this without doint set -k before running the script. or is there something that i can do in the script where i can use value of the parameter given while running the script in the below way
test_sh XYZ=KLMN
i am using ksh.
Any help is appreciated.
How about running this?
XYZ=KLMN ./test_sh //running from directory where test_sh is
If your script needs no other arguments, a quick and dirty way do to it is to put
eval "$#"
at the start of your script. This will evaluate the command line arguments as shell commands. If those commands are to assign a shell/environment variable, then that's what it will do.
It's quick-and-dirty since anything could be put on the command line, causing problems from a syntax error to a bad security hole (if the script is trusted).
I'm not sure if "$#" means the same in ksh as it does in bash - using just $* (without quotes) would work too, but is even dirtier.
It looks like you are trying to use the environment variable "INSTANCE" in your script.
For that, the environment variable must be set in advance of executing your script. Using the "set" command sets exportable environment variables. Incidentally, my version of ksh dates from 1993 and the "-k" option was obsolete back then.
To set an environment variable so that it is exported into spawned shells, simply use the "export" command like so:
export INSTANCE='whatever you want to put here'
If you want to use a positional parameter for your script -- that is have the "KLMN" value accessed within your script, and assuming it is the first parameter, then you do the following in your script:
#!/bin/ksh
echo $1
You can also assign the positional parameter to a local variable for later use in your script like so:
#!/bin/ksh
param_one=$1
echo $param_one
You can call this with:
test_sh KLMN
Note that the spacing in the assignment is important -- do not use spaces.
I am tring this option
#!/bin/ksh
echo $1
awk '{FS="=";print $2}' $1
and on the command line
test_sh INSTANCE=LSN_MUM
but awk is failing.is there any problem over here?
Probably #!/bin/ksh -k will work (untested).