Unable to test whether a file exists in Bash [duplicate] - bash

This question already has an answer here:
Why isn't tilde (~) expanding inside double quotes? [duplicate]
(1 answer)
Closed 4 years ago.
Consider the following Bash script:
function dosomething {
local fname="~/.bash_profile"
if [[ -f "$fname" ]]; then
echo "proceeding"
else
echo "skipping"
fi
}
dosomething
I always get "skipped" although I know that ~/.bash_profile exists. Why?

~ is only expanded by the shell if it's unquoted. When it's quoted it's a literal tilde symbol.
local fname=~/.bash_profile

Related

Meaning of "+x" in shell parameter expansion [ -z ${FILE+x}] [duplicate]

This question already has answers here:
What does "plus colon" ("+:") mean in shell script expressions?
(4 answers)
Closed 5 years ago.
I have a bash script that uses the following syntax:
if [ ! -z ${ARGUMENT+x} ]; then
What is the meaning of the "+x" syntax after the argument name?
It means that if $ARGUMENT is set, it will be replaced by the string x
Let's try in a shell :
$ echo ${ARGUMENT+x}
$ ARGUMENT=123
$ echo ${ARGUMENT+x}
x
You can write this with this form too :
${ARGUMENT:+x}
It have a special meaning with :, it test that variable is empty or unset
Check bash parameter expansion
Rather than discussing the syntax, I'll point out what it is attempting to do: it is trying to deterimine if a variable ARGUMENT is set to any value (empty or non-empty) or not. In bash 4.3 or later, one would use the -v operator instead:
if [[ -v ARGUMENT ]]; then

Bash script outputs unexpected directory display - why? [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed last year.
The following outputs the directory display for the tmp directory.
Why, and how to stop it.
#!/bin/bash
cd /tmp
echo '*' >zzz
cat zzz
IFS='' read something <zzz
echo ${something}
Quote your expansions.
echo "${something}"
See Parameter Expansion, Word Splitting and Filename Expansion.
The shell scans the results of parameter expansion, command
substitution, and arithmetic expansion that did not occur within
double quotes for word splitting.

How to write a variable in a script from another script without sending the value of the variable? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
Basically, I want to send a variable as $1 in another script without the value it has saved.
#!/bin/bash
echo -e "#!/bin/bash\ncp ~/src/$1" > ~/asset/newfile.sh
So, that in the file newfile.sh it is written:
#!/bin/bash
cp ~/src/$1
You can escape the dollar sign with a backslash:
echo -e "#!/bin/bash\ncp ~/src/\$1"
Or, switch to single quotes:
echo -e '#!/bin/bash\ncp ~/src/$1'

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.

What does ${VARIABLE+x} mean in bash? [duplicate]

This question already has answers here:
How to check if a variable is set in Bash
(38 answers)
Closed 5 years ago.
I've come across code that uses this syntax in an if condition:
if [ ! -z ${VARIABLE+x} ]; then
some commands here
fi
Does it test for an non-empty variable? If so, how is it different from ! -z "$VARIABLE"?
See PARAMETER EXPANSION in man bash:
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
And few paragraphs above in the same section:
Omitting the colon results in a test only
for a parameter that is unset.

Resources