Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.debian.user > #268572
| From | Greg Wooledge <greg@wooledge.org> |
|---|---|
| Newsgroups | linux.debian.user |
| Subject | Re: variables in bash |
| Date | 2024-03-28 12:20 +0100 |
| Message-ID | <ImVXz-2fAH-1@gated-at.bofh.it> (permalink) |
| References | <ImUoN-2eiy-9@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
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."
Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
variables in bash Hans <hans.ullrich@loop.de> - 2024-03-28 10:40 +0100
Re: variables in bash Greg Wooledge <greg@wooledge.org> - 2024-03-28 12:20 +0100
[SOLVED] Re: variables in bash Hans <hans.ullrich@loop.de> - 2024-03-28 14:10 +0100
Re: variables in bash Paul M Foster <paulf@quillandmouse.com> - 2024-03-28 12:30 +0100
csiph-web