Error concatenating directory path in bash script
Tilde (~
) will not be expanded by shell when put inside quotes. Just remove the quotes :
#!/bin/bash
dir_path=~/Desktop/param_bind_b
cd "$dir_path"
An other solution is, place only the ~
outside the quotes or use $HOME
instead. Additionally you should add || exit
behind cd
.
#!/bin/bash
dir_path=~"/Desktop/param_bind_b"
cd "$dir_path" || exit
Or
#!/bin/bash
dir_path="$HOME/Desktop/param_bind_b"
cd "$dir_path" || exit
so you can use other variables, E.G.
#!/bin/bash
desktop_dir="/Desktop"
dir_path=~"$desktop_dir/param_bind_b"
cd "$dir_path" || exit
or
#!/bin/bash
desktop_dir="/Desktop"
dir_path=~"$desktop_dir"/param_bind_b
cd "$dir_path" || exit
or
#!/bin/bash
desktop_dir="/Desktop"
dir_path="$HOME$desktop_dir"/param_bind_b
cd "$dir_path" || exit
In the future, check your scripts here. ;)