Passing bash variables not working [duplicate] - bash

This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Closed 7 years ago.
I have the following set in bash:
export chain_dir='~/.eris/chains/simplechain'
This works
echo $chain_dir
~/.eris/chains/simplechain
Then try I try to use it with ls and I get:
ls $chain_dir
ls: cannot access ~/.eris/chains/simplechain: No such file or directory
But cut and paste the same directory string as in $chain_dir and it works:
~/.eris/chains/simplechain
accounts.csv addresses.csv simplechain_full_000 simplechain_root_000
I believe that I am missing something:

~ isnt expanded into your home dir before giving it to ls.
Use $HOME or
leteval do the expansion for you
$var1='~/.config/'
$ls $(eval echo "$var1")

The ~ isn't expanded to your home directory path before being passed to ls.
I would suggest writing it out in full, /home/user/, or using $HOME:
export chain_dir="$HOME/.eris/chains/simplechain"
Note the use of double quotes around the string.

~ isn't being expanded because you quoted it. Leave it outside the quotes, and bash will expand it before performing the assignment. Notice that you can quote the rest of the path if necessary, as long as the ~ is outside the quotes.
$ chain_dir=~'/.eris/chains/simplechain'
$ echo "$chain_dir"
/home/tbrooke/.eris/chains/simplechain
(Assuming /home/tbrooke is your home directory.)

Related

Bash too many arguments error with `cd $VAR`, where $VAR is path name with escaped spaces [duplicate]

This question already has answers here:
How to cd into a directory with space in the name?
(18 answers)
Closed 2 years ago.
I could not cd to a directory, whose path name contains escaped spaces, that is represented by a variable.
The following command does not work.
~$ VAR="/mnt/e/documents/my\ files"
~$ cd $VAR
-bash: cd: too many arguments
However, when I use the path name as it is instead of assigning it to a variable, it works.
~$ cd /mnt/e/documents/my\ files
cd /mnt/e/documents/my files$
How to use cd with a variable as its argument, where the variable is a string with spaces that are escaped.
You have two problems:
Remove the backslash in the middle of my\ files. You only need to escape that space when you're not quoting it.
Add quotes around $VAR: cd "$VAR", otherwise it looks as though you are providing two parameters: "/mnt/e/documents/my" and "files"

Tilde not expanded when quoting on the right hand side of a Bash variable assignment [duplicate]

This question already has answers here:
Why isn't tilde (~) expanding inside double quotes? [duplicate]
(1 answer)
Tilde expansion in quotes
(3 answers)
Tilde in path doesn't expand to home directory
(5 answers)
Closed 4 years ago.
I have made a directory ~/test_myDir
I then run the following bash script:
x="myDir"
dirName="~/test_$x"
cd $dirName
echo "hey" > test.txt
I get the following error:
test.sh: line 5: cd: ~/test_myDir: No such file or directory
I then remove the quotes from the second assignment:
x="myDir"
dirName=~/test_$x
cd $dirName
echo "hey" > test.txt
The script runs without error.
What is going on here? I ran into this issue in a larger, more complicated script, and I narrowed it down to my use of quotes in a variable assignment that contained another variable.
Still, from the error message, it looks like the full path is being expanded correctly in the "cd" call.
Quotation marks prevent expansion of ~. Replace ~ with $HOME or use dirName=~/"test_$x".
From the manual's explanation of tilde expansion:
Each variable assignment is checked for unquoted tilde-prefixes immediately following a : or the first =. In these cases, tilde expansion is
also performed.

bash diff fails to found existing files when providing a path variable containing tilde (~) [duplicate]

This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Closed 4 years ago.
I have a very surprising problem while trying to execute a diff command inside a bash script.
Here is a working code illustrating the point:
#!/bin/bash
cd
mkdir foo bar
head -c 1024 /dev/urandom >foo/qux
head -c 1024 /dev/urandom >bar/qux
# works properly as expected
diff ~/{foo,bar}/qux
folder="~"
# this fails with the ~ inside a variable
diff $folder/{foo,bar}/qux
# cleaning the mess
rm -rf foo bar
So my question is:
Why ? :-)
Don't quote the ~ when assigning it to a variable. The ~ is only expanded by bash when you don't quote it.
~ is a feature of shell expansion.
Double quotes limit the expansion to only three features:
Command substitution: $(some command here) or `some command here`
Variable substitution: $VAR or ${VAR}
Arithmetic: $((2+2))
so when put inside double quotes, the ~ is not expanded
Tilde expansion only applies to unquoted tildes. The tilde must be expanded at the time you perform the assignment to folder, because tilde expansion is not applied to parameter expansions, only word splitting and pathname expansion.
folder=~ # ~ is expanded, and the result is assigned to folder

Bash: variable not getting expanded properly [duplicate]

This question already has answers here:
Filename not printing correctly with underscore "_" in Bash [duplicate]
(2 answers)
Closed 6 years ago.
I am trying to use variable in renaming a file. However, I when insert the variable to the beginning of the filename, things does not work as expected.
Here's the case, I have a file name test:
$ ls
test
and a variable
i=1
When adding the variable to the end or middle of filename, it works:
$ mv test test_$i
$ ls
test_1
When adding the variable to the beginning of filename, it doesn't work:
$mv test_1 test
$mv test $i_test
mv: missing destination file operand after 'test'
Try 'mv --help' for more information.
And even worse, when there is extension in my filename, the file will be removed.
$ touch test.try
$ ls
test.try
$ mv test.try $i_test.try
$ ls
(nothing!)
Can anyone explain this to me? Is it a bug or something I don't know?
You need to put {} around the variable name to disambiguate it from the rest of the literal (remember, _ is a valid character in an identifier):
mv test.try ${i}_test.try
or, use double quotes, which gives you protection against word splitting and globbing:
mv test.try "${i}"_test.try
In your code:
$i_test => shell treats "i_test" as the variable name
$i_test.try => shell treats "i_test" as the variable name ('.' is not a valid character in an identifier)
mv test.try $i_test.try => test.try got moved to .try as "$i_test" expanded to nothing. That is why ls didn't find that file. Use 'ls -a' to see it.
See this related post: When do we need curly braces in variables using Bash?

How to take path from variable Bash [duplicate]

This question already has answers here:
How to manually expand a special variable (ex: ~ tilde) in bash
(19 answers)
Closed 9 years ago.
I can't get path from variable in bash. How do it correct?
For example:
my#PC:~$ a="~/.bashrc"
my#PC:~$ cat $a
cat: ~/.bashrc: No such file or directory
didn't work, but
cat .bashrc
and
cat ".bashrc"
Works well.
Here is right answer from fedorqui
cat $(eval echo $a)
The reason for the issue is that the tilde is expanded to the home directory by the shell. When you store it in a variable, the tilde is not expanded and cat looks for a file .bashrc in the folder ~ (rather than your home directory)
There are two ways around the issue: the proposed eval, and using $HOME:
a="$HOME/.bashrc"

Resources