Copy value of 1 variable into another variable in UNIX - linux

New to Unix not aware of the syntax structure so please excuse my syntax brevity.I am trying to copy a value of a variable and store that in another variable eg:
Two variables:
abc
bcd
Given:
abc=123
I want to copy the contents of abc i.e 123 in bcd. How to achieve this in Unix?
Earlier I was trying to copy the contents of abc in a .txt file which was working for me: see the code snippet below:
abc='123'
echo $abc >>/data/test/tt.txt
But know I want to copy them in another variable so I tried to do the following but was of no success.
abc='123'
test=`echo $abc>>bcd`
echo $test
Can you assist me in this?

Easy:
bcd="$abc"
For example:
abc="hello world"
The quotes there are necessary or else it will try to run a command named world with abc in its environment.
Actually, the quotes are not necessary (thanks to 1_CR for pointing this), but I like to add them for readability:
bcd=$abc
bcd="$abc"
They both do the same, exactly what you need.
Lastly, do not use single quotes, or else you will not get the value of the variable:
bcd='$abc'
Error! Now your bcd variable contains the literal value $abc.

Related

How to concatenate two variables from azure variable group and assign them to one with single quotes

I'm having 2 Variables inside a variable group and their values are as shown below:
cr 2200
tr cd1200
I would like to assign these two variables to a third variable cd as shown below:
cd '2200;cd1200;abc.txt'
I'm trying to use below script but it is showing too many arguments.
Can someone please help in this.
I'm using below code:
cd = \'"$cr";"$tr";abc.txt\'
echo "$cd"
I need output as:
cd = '2200;cd1200;abc.txt'
Using Azure macro syntax ($(<varName>)):
$cd = '$(cr);$(tr);abc.txt'
Note:
PowerShell always needs the $ sigil when accessing a variable - even when assigning to it (unlike in POSIX-compatible shells such as Bash).
Azure's macro syntax - which textually expands references to Azure variables up front, before PowerShell sees the code, is not be confused with PowerShell's subexpression operator ($(...))

Get current branch name

I'm running a script (bitbucket_pipelines.yml) and on one of the steps I need to know the current branch name, How can I get it?
I saw there is a predefined BITBUCKET_BRANCH variable, but I'm having troubles to print it so I can see its content.
I tried to do:
...
step:
script:
- echo $BITBUCKET_BRANCH
but when pipelines runs all I see is
echo $BITBUCKET_BRANCH
How can I really see the content of this variable?
I found that Bb Pipelines are sometimes picky when dealing with variables. Try changing this to echo "$BITBUCKET_BRANCH". Also, enclosing the whole line in single quotes might help.
#Shvalb, the question should be how to display the value of a variable in bitbucket pipeline.
I deal with bitbucket support on this matter before.
I want to echo a repo/pipeline variable to see the value and it is not showing correctly.
In my case, it was my repo variable conflict with my deployment/pipeline variable. However, from the support, I understand bitbucket is using search and replace the screen value to "hide" the actual value of the variable with direct echo.
in order to see the value, you can use
echo $VAR > /tmpfile
cat /tmpfile
It was the trick I used before but I am not sure whether it will still work.

How to Read a variable in defined in terminal to a bash script

I am new to shell scripting. I have a very basic question about how we read variables defined in our terminal as input to a shell scirpt.
let us say i defined this variable in my terminal
a=22
if i do echo $a it gives 22 as output in my terminal.
I wanted to pass this variable as a parameter to a script named input.sh
#!/bin/bash
echo "Enter variable name:"
read Input
echo $Input
I ran the scirpt as ./input.sh It popped up the message as
Enter variable name:$a
But in the output i have $a as output not 22. I wanted 22 as output. Is there a way to do this?
You are mixing several things here. Where to start...
The usual way to pass values to a script is through positional parameters.
Suppose you have this script, called s1 :
#!/bin/bash
echo "First two args are: $1 $2"
If you execute it like this :
./s1 Arg1 Arg2
You will see the following output :
First two args are: Arg1 Arg2
If you want to pass a variable name to the script, and have that script output the value of this variable, then you must do two things. First, initialize the variable and export it so that it can be seen by the children processes (including the script you will call, which is a separate process unless called with source or .).
VAR="Some value"
export VAR
You can also do both in a single statement:
export VAR="Some value"
Then, adapt the script to perform an indirect access to the variable :
#!/bin/bash
echo "Value of variable named $1 : ${!1}"
Please note that while $1 means "the content of variable 1", ${!1} means "the content of the variable that is named $1". This is the indirect part.
Now, if you want to take it a step further, and allow the script to interactively read user input (not an argument) and use the value read as a variable name to expand, you would do something like this :
#!/bin/bash
echo "Please enter a variable name"
read VARNAME
echo "Value of variable named $VARNAME : ${!VARNAME}"
Using positional parameters makes the script easier to reuse in non-interactive scenarios, so reading user input should be limited to cases where it is necessary.
The above is to help understand the basics. If you move beyond toy scripts, you will need to understand the security implications of indirect access (especially if you allow user input). You will also need to validate positional parameters or user input are valid for your purpose (i.e. contains a valid variable name) so that you may have your script react appropriately. Well, you would probably need to check if positional parameters were even provided to begin with. All of this is doable in shell scripting, but is beyond the scope of a single question. In any case, checking input (and also errors) will be required if you intend to have robust scripts in situations where reliability is expected.
To use the variables defined in the terminal in your bash script -
Change last line of your script so that it looks like -
#!/bin/bash
echo "Enter variable name:"
read Input
echo ${!Input}
Run your script input.sh on the terminal as -
. input.sh
And finally, when you input the variable name, do not use $ sign. For e.g. -
6c4008a16b7c:~ z001lg8$ . input.sh
Enter variable name:
a
22
6c4008a16b7c:~ z001lg8$
Voila, you can now use the variables defined in the terminal in your script.
Explanation -
In your script, $Input is changed to ${!Input} so that the content of the user input(which is variable name) is echoed and not the variable name itself.
As explained by #Fred - $1 means "the content of variable 1", ${!1} means "the content of the variable that is named $1".
When script is run as . input.sh , it means that you are sourcing the script contents on the terminal. The . symbol is used for sourcing a command/script.
The $ sign is not required when the variable name is entered in terminal because ${!Input} format already takes into account that the value in Input variable is a variable name.

Part of name of a variable to be a variable

Let's suppose I have following two variables:
arg1=5
count5="test"
Now, I want to able to do something like:
echo ${'count' . $arg1} #which should give me "test" but its returning bad-substitution error
i.e. can part of a variable can be set as variable in bash?
Okay, got it working via indirect variable reference introduced in Bash v2 as:
my_var="count$arg1"
echo ${!my_var}

Bash, Concatenating 2 strings to reference a 3rd variable

I have a bash script I am having some issues with concatenating 2 variables to call a 3rd.
Here is a simplification of the script, but the syntax is eluding me after reading the docs.
server_list_all="server1 server2 server3";
var1 = "server";
var2 = "all";
echo $(($var1_list_$var2));
This is about as close as I get to the right answer, it acknowledges the string and tosses an error on tokenization.
syntax error in expression (error token is "server1 server2 server3....
Not really seeing anything in the docs for this, but it should be doable.
EDIT: Cleaned up a bit
The Bash Reference Manual explains how you can use a neat feature of parameter expansion to do some indirection. In your case, you're interested in finding the contents of a variable whose name is defined by two other variables:
server_list_all="server1 server2 server3"
var1=server
var2=all
combined=${var1}_list_${var2}
echo ${!combined}
The exclamation mark when referring to combined means "use the variable whose name is defined by the contents of combined"
The Advanced Bash Scripting Guide has the answer for you (http://tldp.org/LDP/abs/html/ivr.html). You have two options, the first is classic shell:
#!/bin/bash
server_list_all="server1 server2 server3";
var1="server";
var2="all";
server_var="${var1}_list_${var2}"
eval servers=\$$server_var;
echo $servers
Alternatively you can use the bash shortcut ${!var}
#!/bin/bash
server_list_all="server1 server2 server3";
var1="server";
var2="all";
server_var="${var1}_list_${var2}"
echo ${!server_var}
Either approach works.

Resources