On Thu, Mar 28, 2024 at 10:37:25AM +0100, Hans wrote:
What is the difference (if any) between the following two variables in a shellfile in bash:
1. mypath=/home/user1/Tools/
2. mypath="/home/user1/Tools/"
They are the same. The quotes are optional here, because your assignment doesn't contain any whitespace.
The quotes would be required if you had something like:
mypath="/mnt/c/Program Files/"
In any case, the assignment is the easy part. Most people get this
part right. Where they screw up is here:
and $mypath
Whenever[1] you use "$mypath", you'll want double quotes around it.
Otherwise, it'll get split into multiple words at whitespace, and
will also be subject to filename expansion. You don't want that.
mypath="/mnt/c/Program Files/"
if ! test -d $mypath; then ... # wrong
The example above will *not* work, because the space in the middle
of $mypath's value will cause the test command to receive two
separate words, instead of one word. It needs to be quoted:
mypath="/mnt/c/Program Files/"
if ! test -d "$mypath"; then ... # right
Is this in bash the same? Do other shells (sh, zsh, whatever) handle these two
different?
Most quoting rules are the same in all sh-derived shells, including the
rules I talked about here.
As you dive deeper into shell scripting, you'll find some cases where
the rules change a bit across different shells, but so far you're still
in the shallow end.
[1] There are some exceptions, but for now, go with the simplest rule:
"When in doubt, double-quote it."
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)