vim opens directories instead of files [duplicate] - bash

This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Closed 4 years ago.
I want to check whether or not the hidden .git folder exists. First thought was to use:
if [ -d "~/.git" ]; then
echo "Do stuff"
fi
But the -d apparently does not look for hidden folders.

The problem has to do with the tilde being within double quotes.
To get it expanded, you need to put the tilde outside the quotes:
if [ -d ~/".git" ]; then # note tilde outside double quotes!
echo "Do stuff"
fi
Or, alternatively, as commented below by hek2mgl, use $HOME instead of ~:
if [ -d "$HOME/.git" ]
From POSIX in Tilde expansion:
A "tilde-prefix" consists of an unquoted <tilde> character at the beginning of a word, followed by all of the characters preceding the first unquoted <slash> in the word, or all the characters in the word if there is no <slash>.
From POSIX in Double Quotes:
Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the exception of the characters dollar sign, backquote, and backslash, as follows:
You can find further explanations in Why doesn't the tilde (~) expand inside double quotes? from the Unix & Linux Stack.

Related

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 pass variable value with double quote in shell script [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
USER_UID=$1
echo 'generate_token("$USER_UID")'
I want output like
generate_token("1234567")
i tried multiple ways but didn't worked. it just print same line without value generate_jwt("$USER_UID")
When you use single quotes, it causes the shell to preserve the literal value of each character within the quotes. This means the $ will be treated as a literal $ character.
You should use double quotes:
USER_UID="$1"
echo "generate_token(\"$USER_UID\")"
From the bash man page, under the Quoting section:
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.
For POSIX details on quoting, see here.
Example in an interactive shell:
$ USER_UID='foo'
$ echo "generate_token(\"$USER_UID\")"
generate_token("foo")
This will also work if USER_UID contains spaces:
$ USER_UID='var with spaces'
$ echo "generate_token(\"$USER_UID\")"
generate_token("var with spaces")

If elif not working when search for a file [duplicate]

This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Closed 4 years ago.
I want to check whether or not the hidden .git folder exists. First thought was to use:
if [ -d "~/.git" ]; then
echo "Do stuff"
fi
But the -d apparently does not look for hidden folders.
The problem has to do with the tilde being within double quotes.
To get it expanded, you need to put the tilde outside the quotes:
if [ -d ~/".git" ]; then # note tilde outside double quotes!
echo "Do stuff"
fi
Or, alternatively, as commented below by hek2mgl, use $HOME instead of ~:
if [ -d "$HOME/.git" ]
From POSIX in Tilde expansion:
A "tilde-prefix" consists of an unquoted <tilde> character at the beginning of a word, followed by all of the characters preceding the first unquoted <slash> in the word, or all the characters in the word if there is no <slash>.
From POSIX in Double Quotes:
Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the exception of the characters dollar sign, backquote, and backslash, as follows:
You can find further explanations in Why doesn't the tilde (~) expand inside double quotes? from the Unix & Linux Stack.

linux bash script: processing file over folder content iteration with nested For Do Loop [duplicate]

This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Closed 4 years ago.
I want to check whether or not the hidden .git folder exists. First thought was to use:
if [ -d "~/.git" ]; then
echo "Do stuff"
fi
But the -d apparently does not look for hidden folders.
The problem has to do with the tilde being within double quotes.
To get it expanded, you need to put the tilde outside the quotes:
if [ -d ~/".git" ]; then # note tilde outside double quotes!
echo "Do stuff"
fi
Or, alternatively, as commented below by hek2mgl, use $HOME instead of ~:
if [ -d "$HOME/.git" ]
From POSIX in Tilde expansion:
A "tilde-prefix" consists of an unquoted <tilde> character at the beginning of a word, followed by all of the characters preceding the first unquoted <slash> in the word, or all the characters in the word if there is no <slash>.
From POSIX in Double Quotes:
Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the exception of the characters dollar sign, backquote, and backslash, as follows:
You can find further explanations in Why doesn't the tilde (~) expand inside double quotes? from the Unix & Linux Stack.

Why isn't tilde (~) expanding inside double quotes? [duplicate]

This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Closed 4 years ago.
I want to check whether or not the hidden .git folder exists. First thought was to use:
if [ -d "~/.git" ]; then
echo "Do stuff"
fi
But the -d apparently does not look for hidden folders.
The problem has to do with the tilde being within double quotes.
To get it expanded, you need to put the tilde outside the quotes:
if [ -d ~/".git" ]; then # note tilde outside double quotes!
echo "Do stuff"
fi
Or, alternatively, as commented below by hek2mgl, use $HOME instead of ~:
if [ -d "$HOME/.git" ]
From POSIX in Tilde expansion:
A "tilde-prefix" consists of an unquoted <tilde> character at the beginning of a word, followed by all of the characters preceding the first unquoted <slash> in the word, or all the characters in the word if there is no <slash>.
From POSIX in Double Quotes:
Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the exception of the characters dollar sign, backquote, and backslash, as follows:
You can find further explanations in Why doesn't the tilde (~) expand inside double quotes? from the Unix & Linux Stack.

Resources