Bash - Concatenating Variable on to Path

Bash - Concatenating Variable on to Path

Tilde expansion doesn't work when inside a variable. You can use the $HOME variable instead:

#!/bin/bash
p=$HOME
tar xvf "$p/some_file.tar"

File path that modified by concatenating variables not working

If you have a value "between quotes", then you do not need to escape\ spaces. So, change each \ into just .

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.

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

Concatinating paths in bash (.sh) resolves in path with '$'\r''

You can use:

part1="${part1/$'\r'}/numpy"

Here "${part1/$'\r'} replaces \r by an empty string. $'\r' is special bash construct to enter escape sequences.

Concatenating a string inside a while loop in bash

Just do not use cat - ie. do not use pipe. And do not use USER.

while read var; do
str="${str}${var}"
done < "$file"

Do not use upper case variables in your scripts. USER is variable set by bash to the name of current user.

Check scripts with http://shellcheck.net

Read https://mywiki.wooledge.org/BashFAQ/024

Read https://mywiki.wooledge.org/BashFAQ/001

Do not uselessly use cat.

In bash you can also use str+="$var".

Quote variable expansions.

Get current directory and concatenate a path

Sounds like you want:

path="$(pwd)/some/path"

The $( opens a subshell (and the ) closes it) where the contents are executed as a script so any outputs are put in that location in the string.


More useful often is getting the directory of the script that is running:

dot="$(cd "$(dirname "$0")"; pwd)"
path="$dot/some/path"

That's more useful because it resolves to the same path no matter where you are when you run the script:

> pwd
~
> ./my_project/my_script.sh
~/my_project/some/path

rather than:

> pwd
~
> ./my_project/my_script.sh
~/some/path
> cd my_project
> pwd
~/my_project
> ./my_script.sh
~/my_project/some/path

More complex but if you need the directory of the current script running if it has been executed through a symlink (common when installing scripts through homebrew for example) then you need to parse and follow the symlink:

if [[ "$OSTYPE" == *darwin* ]]; then
READLINK_CMD='greadlink'
else
READLINK_CMD='readlink'
fi

dot="$(cd "$(dirname "$([ -L "$0" ] && $READLINK_CMD -f "$0" || echo "$0")")"; pwd)"

More complex and more requirements for it to work (e.g. having a gnu compatible readlink installed) so I tend not to use it as much. Only when I'm certain I need it, like installing a command through homebrew.



Related Topics



Leave a reply



Submit