How Connect Variable with a String in Bash

How to concatenate string variables in Bash

foo="Hello"
foo="${foo} World"
echo "${foo}"
> Hello World

In general to concatenate two variables you can just write them one after another:

a='Hello'
b='World'
c="${a} ${b}"
echo "${c}"
> Hello World

How to concat variable and string in bash script

Strings are concatenated by default in the shell.

value="$variable"text"$other_variable"

It's generally considered good practice to wrap variable expansions in double quotes.

You can also do this:

value="${variable}text${other_variable}"

The curly braces are useful when dealing with a mixture of variable names and strings.

Note that there should be no spaces around the = in an assignment.

How connect variable with a string in Bash?

Firstly you need to use single quotes (''') around strings, not backticks ('`')

list='/home/ea/students'

To append a string to a variable, do the following:

list=${list}/admin.txt

Demo:

echo $list
/home/ea/students/admin.txt

How to concatenate string variables in a Bash script

This is a problem with mixed Unix and Windows line endings. You have a carriage return at the end of the MY_DIRECTORY= line. The carriage return is causing part of the second output line to be overwritten. I can replicate your output on my Cygwin installation.

If you run the script through dos2unix it will work as expected.

To prevent this kind of thing happening again, configure your text editor to save files using Unix line endings.

Concatenating variables in Bash

Try doing this, there's no special character to concatenate in bash :

mystring="${arg1}12${arg2}endoffile"

explanations

If you don't put brackets, you will ask bash to concatenate $arg112 + $argendoffile (I guess that's not what you asked) like in the following example :

mystring="$arg112$arg2endoffile"

The brackets are delimiters for the variables when needed. When not needed, you can use it or not.

another solution

(less portable : require bash > 3.1)

$ arg1=foo
$ arg2=bar
$ mystring="$arg1"
$ mystring+="12"
$ mystring+="$arg2"
$ mystring+="endoffile"
$ echo "$mystring"
foo12barendoffile

See http://mywiki.wooledge.org/BashFAQ/013

How to concatenate to string variable from inside function in bash script?

With Bash 4.3+ (namerefs!)

Modern versions of bash provide "nameref" support, which allows a variable to refer to another.

#!/usr/bin/env bash

case $BASH_VERSION in
[123].*|4.[012].*) echo "This needs bash 4.3 or newer" >&2; exit 1;;
esac

# local variable is prefixed because this will fail if we're passed a variable name we use
# internally; prefixing the local names makes such collisions unlikely.
change_string() {

# make change_string__var an alias for the variable named in our argument
declare -n change_string__var=$1

# append to that variable
change_string__var+=",Hello2"

# ...and log the new value, 'cuz that's what the original code did.
echo "Function: $change_string__var"
}

myString="Hello1"
change_string myString ## pass variable **name**, not variable value
echo "Main: $myString"

With Bash 3.x+ (indirect evaluation + indirect assignment)

Alternately, as a less-newfangled approach, one can use ${!var} to do an indirect reference, and printf -v to do an indirect assignment.

#!/usr/bin/env bash

change_string() {

# Store the variable name in a regular local variable
local change_string__var=$1

# Use ${!var} to get the value of the variable thus named
local change_string__val=${!change_string__var}

# Use ''printf -v varname ...'' to assign a new value
printf -v "$change_string__var" %s "${change_string__val},Hello2"
}

myString="Hello1"
change_string myString
echo "Main: $myString"

Bash, Concatenating 2 strings to reference a 3rd variable

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"

Concatenate inputs in string while in loop

SOURCES="a b c d e"
DESTINATIONS=""

for src in $SOURCES
do
echo Input destination to associate to the source $src:
read dest
DESTINATIONS+=" ${dest}"
done
echo $DESTINATIONS

works for me.



Related Topics



Leave a reply



Submit