Bash 101 - string assigment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash 101 - string assigment
# 1  
Old 08-26-2018
Bash 101 - string assigment

I am embarrassed to ask this but I am at the end of my rope.
I am trying to assign one (string) variable to another.
I do understand that bash variables are "not typed", but still learning usage of {} and spaces in script.



I was hoping this syntax would work



$variableA = $variableB


Here is a snippet of the output



Code:
474 lsusb save result command into custom menu
+ echo lsusb
lsusb
+ echo '477 lsusb
user input transfered '
477 lsusb
user input transfered 
+ CLI command=lsusb
/usr/bin/raspi-config-DEBUG.sh: line 478: CLI: command not found
+ echo 'CLI command'


failed here 

the "command" should be "lsusb" not "CLI command" 





CLI command
+ echo '480  CLI command new choice '
480  CLI command new choice 
+ echo 'CLI command'
CLI command
+ INPUT_BOX_TASK='Add command description to custom menu '


And a snippet of the code



Code:
INPUT_BOX_TITLE="New custom menu command"
 473 inputBox
 474 echo "$LINENO $USERINPUT save result command into custom menu" 
 475 echo  $USERINPUT
 476 echo "$LINENO $USERINPUT Y
 477 user input transfered " 
 478 $choice=$USERINPUT 
 479 echo "$choice"
 480 echo "$LINENO  $choice new choice "
 481 echo "$choice"


I did try



{$choice}={$USERINPUT}


without success
# 2  
Old 08-26-2018
Code:
choice=${USERINPUT}
# or
choice="$USERINPUT"

will work. Whenever you reference a string try to put double quotes around it. Or use the ${variable} syntax for the source variable, not the destination
# 3  
Old 08-26-2018
Quote:
Originally Posted by annacreek
Code:
{$choice}={$USERINPUT}

You got it - almost - right, but still wrong. ;-))

Here is how "variable expansion" (this is how the process is called) works:

if you prepend a string with the dollar sign ("$"), the string will be interpreted as name of a variable and the name will be replaced by the content of the variable. Example:

Code:
echo $myvar

This will replace the string "$myvar" with the content of the variable "myvar" and then execute the echo-command with it.

Note that you do NOT need this "expansion" when you assign a variable! Hence:

Code:
myvar="bla bla"
echo $myvar

Also notice, that this expansion notation is a shortcut: the complete construct is to enclose the name in curly braces. The following illustrates this:

Code:
myvar="this is a test"
echo $myvar
echo ${myvar}

Now, that leaves the question of how to assign one variable with the content of another. Can you guess it?

Code:
myvar="this is myvar"
newvar="$myvar"
echo $newvar
echo ${newvar}
newervar="${myvar}"
echo $newervar
echo ${newervar}

A word about quoting: you will notice that i enclosed the assignment into double quotes. This is because the shell automatically splits at word boundaries. This would be what happens without quoting:

Code:
myvar=multiple words

The shell would assign "multiple" to the variable "myvar" and be left over with the word "words", which would lead to an error message, because it simply makes no sense. The same with

Code:
newvar=$myvar

First the shell would replace "$myvar" with the content of the variable "myvar" ending with this line:

Code:
newvar=multiple words

and here what i said above applies. This is why you need quoting, which is a bigger topic in itself, so i am cutting it short here. Just ask if you would like to know more about this.

Also notice that expansion is not limited to simple expanding to the unaltered content. It is also possible to change on the expanded content of the variable:

Code:
newvar=${myvar}
echo ${myvar#????????}      # cut off the first 8 characters
echo ${myvar%??????}        # cut off the last 6 characters
echo ${myvar%??????}        # cut off the last 6 characters
echo ${myvar/words/WORDS}  # replace a certain part with something else

and so on. I suggest a good book about shell programming for this.

I hope this helps.

bakunin
# 4  
Old 08-26-2018
Thanks for replay and very comprehensive explanation of the "variable expansion" process.
Appreciate that very much.



Perhaps I need to find "bash dictionary of terms" to help Mrs Google to at least give me a hint what to look for in future. I was looking for "variable assignments" in this case and it just did not help me.

Thanks
# 5  
Old 08-27-2018
To get familiar with bash terminology, it would already help to glance over the bash man page, in particular to the titles of the respective chapters. You don't need to understand every detail yet, but at least you know next time when you are asking a question, how the different concepts of bash are called.
# 6  
Old 08-27-2018
Very useful suggestion. I believe my "problem" is hacking "inherited" script. Since it is my first bash scrip I just "fix" what I need and most of the time do not spent much time to fully analyze / understand what I am hacking. I am using bash because the script is there and all I need to modify / add few lines of code. I find having coded in C/C++ makes it harder to grasp bash conceptually.

Especially "usage" of "spaces" drives me nuts!





And I really appreciate this forum help, thanks.
# 7  
Old 08-27-2018
Be careful with this approach! If you don't understand what you are doing, you can easily end up with code which produces no error and works fine with your current set of data, and blows up or does something strange if your data look a little bit different (files missing, spaces in filenames and so on). Not nice if, for instance, half of your files get deleted because of a mistake in your program. Happens much easier in bash than in C++. Better ask here in the forum, if there is a construct which you don't understand.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

2. Shell Programming and Scripting

Bash 101 - to (do) ; or not to (do) ; ?

I figured this forum needs some laughs , so I am posting this. And if the answer is - it depends on bash version - do not reply. This is from "manual" while CONTROL-COMMAND; do CONSEQUENT-COMMANDS; done And here is the REAL code - no ";" while do xterm & i=$ done (2 Replies)
Discussion started by: annacreek
2 Replies

3. Shell Programming and Scripting

Get distinct value in bash between two string

Sorry for my english i am french So i am receiving from a script this prompt : tabular;critical;mirroring;DG INTlocaldg VOLUME appears tabular;critical;mirroring;DG INTlocaldg VOLUME bh3vm tabular;critical;mirroring;DG INTlocaldg VOLUME dev tabular;critical;mirroring;DG INTlocaldg VOLUME... (3 Replies)
Discussion started by: cterra
3 Replies

4. Shell Programming and Scripting

Bash: Getting first digit of a string

If i'm given a string like "abc-def-1.2.3", how would I return "1"? I'm new to scripting and got stumped on this problem. Thanks in advance! (7 Replies)
Discussion started by: atsim
7 Replies

5. Shell Programming and Scripting

Assignment with variable assigment

Hello All, I'm trying to assign integer values to variables using substitution in both the value and variable's name, i.e., number$x=$x where x is equal to one in the first assignment, two in the second assignment, and so on with x being incremented each time. However, when I do the... (7 Replies)
Discussion started by: tombombadil
7 Replies

6. Shell Programming and Scripting

Bash Integers/String

Hy friends, I am newbie to bash scripting, can anyone explain how b=${a/23/BB} # Substitute "BB" for "23". this line converts "b" into string and and "d" into Integer. Thanks in advance (4 Replies)
Discussion started by: Qazi
4 Replies

7. UNIX for Dummies Questions & Answers

Replace a String using Bash

I'm going freakin crazy here! I've tried multiple attempts and configurationa and cannot get this to work. I have a file: private/etc/apt/sources.list.d/cydia.list I want to replace a string in this file: "deb http:name.of.address ./" with "deb http:name.of.other.address ./" The... (4 Replies)
Discussion started by: thazsar
4 Replies

8. Shell Programming and Scripting

variable assigment not works in shell script

Hi, The following assigment is not working within shell script but is working from command line. Could anybody advise why? OS - solaris 8 APPL=`grep "$Application" ldapapps |awk '{print $1}'` echo $APPL (5 Replies)
Discussion started by: urello
5 Replies

9. Shell Programming and Scripting

Bash string manipulation

In my script I'm retrieving a parameter through an API call. I need to trim some things out of the result so I can use it as a parameter to pass to another process. I've got it working but it's pretty kludgy and I'm hoping someone can help me with a better way. This is the code that retrieves... (2 Replies)
Discussion started by: withanh
2 Replies

10. Shell Programming and Scripting

String comparison using bash

I have a program to create a directory if not present. Here is the program. FYI: Directory name format: YYYY_MM_DD #!/bin/bash date=`date +%Y_%m_%d` presence=$(ls -lrt /TS_File/ | grep "$date" | awk '{print $9}') if then mkdir /TS_File/$date else echo "Unable to Create... (5 Replies)
Discussion started by: nthiruvenkatam
5 Replies
Login or Register to Ask a Question