Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #268568 > unrolled thread

variables in bash

Started byHans <hans.ullrich@loop.de>
First post2024-03-28 10:40 +0100
Last post2024-03-28 12:30 +0100
Articles 4 — 3 participants

Back to article view | Back to linux.debian.user


Contents

  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

#268568 — variables in bash

FromHans <hans.ullrich@loop.de>
Date2024-03-28 10:40 +0100
Subjectvariables in bash
Message-ID<ImUoN-2eiy-9@gated-at.bofh.it>
Hi folks,

just an easy question:

What is the difference (if any) between the following two variables in a 
shellfile in bash:

1. mypath=/home/user1/Tools/

and $mypath

or
2. mypath="/home/user1/Tools/"

and $mypath

Is this in bash the same? Do other shells (sh, zsh, whatever) handle these two 
different?

Thanks for any answer, can be short!

Best regards

Hans


  

[toc] | [next] | [standalone]


#268572

FromGreg Wooledge <greg@wooledge.org>
Date2024-03-28 12:20 +0100
Message-ID<ImVXz-2fAH-1@gated-at.bofh.it>
In reply to#268568
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."

[toc] | [prev] | [next] | [standalone]


#268575 — [SOLVED] Re: variables in bash

FromHans <hans.ullrich@loop.de>
Date2024-03-28 14:10 +0100
Subject[SOLVED] Re: variables in bash
Message-ID<ImXG2-2gZ5-5@gated-at.bofh.it>
In reply to#268572
Hi, 

thank you all for the fast response. It helped a lot and made everything 
clear.

The problem is solved.

Have a nice eastern.

Best

Hans

[toc] | [prev] | [next] | [standalone]


#268573

FromPaul M Foster <paulf@quillandmouse.com>
Date2024-03-28 12:30 +0100
Message-ID<ImW7g-2fFJ-3@gated-at.bofh.it>
In reply to#268568
On Thu, Mar 28, 2024 at 10:37:25AM +0100, Hans wrote:

> Hi folks,
> 
> just an easy question:
> 
> What is the difference (if any) between the following two variables in a 
> shellfile in bash:
> 
> 1. mypath=/home/user1/Tools/

Here you are assigning a value to the variable "mypath". You can surround
"/home/user1/Tools" with quotes if you like, but it is not strictly
necessary.

> 
> and $mypath

Here you are actually *using* the variable "mypath".

Here is another example:

NAME="paulf"
echo "My name is $NAME."

which will yield:

My name is paulf.


-- 
Paul M. Foster
Personal Blog: http://noferblatz.com
Company Site: http://quillandmouse.com
Software Projects: https://gitlab.com/paulmfoster

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web