• variables in bash

    From Hans@21:1/5 to All on Thu Mar 28 10:40:01 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul M Foster@21:1/5 to Hans on Thu Mar 28 12:30:02 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to Hans on Thu Mar 28 12:20:01 2024
    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)
  • From Hans@21:1/5 to All on Thu Mar 28 14:10:02 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)