Skip to main content
added 6 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244

My man page says:

$0 : expands to the name of shell or shell script.

It seems this translates to argv[0] of the current shell - or the first nonoperand commandline argument the currently interpreting shell is fed when invoked. I previously stated that sh ./somescript would path its $0 $ENV variable to sh but this was incorrect because the shell is a new process of it' sits own and invoked with a new $ENV$ENV.

In this way sh ./somescript.sh differs from . ./somescript.sh because itwhich runs in the current environment and $0 is already set.

You can check this by comparing $0 to /proc/$$/status.

echo 'script="/proc/$$/status"
    echo $0
    cat "$script"' \
    > ./script.sh
sh ./script.sh ; . ./script.sh

Thanks for the correction, @toxalot. I learned something.

My man page says:

$0 : expands to the name of shell or shell script.

It seems this translates to argv[0] of the current shell - or the first nonoperand commandline argument the currently interpreting shell is fed when invoked. I previously stated that sh ./somescript would path its $0 $ENV variable to sh but this was incorrect because the shell is a new process of it' s invoked with a new $ENV.

In this way sh ./somescript.sh differs from . ./somescript.sh because it runs in the current environment and $0 is already set.

You can check this by comparing $0 to /proc/$$/status.

echo 'script="/proc/$$/status"
    echo $0
    cat "$script"' \
    > ./script.sh
sh ./script.sh ; . ./script.sh

Thanks for the correction, @toxalot. I learned something.

My man page says:

$0 : expands to the name of shell or shell script.

It seems this translates to argv[0] of the current shell - or the first nonoperand commandline argument the currently interpreting shell is fed when invoked. I previously stated that sh ./somescript would path its $0 $ENV variable to sh but this was incorrect because the shell is a new process of its own and invoked with a new $ENV.

In this way sh ./somescript.sh differs from . ./somescript.sh which runs in the current environment and $0 is already set.

You can check this by comparing $0 to /proc/$$/status.

echo 'script="/proc/$$/status"
    echo $0
    cat "$script"' \
    > ./script.sh
sh ./script.sh ; . ./script.sh

Thanks for the correction, @toxalot. I learned something.

added 47 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244

In short,My man page says:

$0 : expands to the name of shell or shell script.

It seems this translates to the bestargv[0] of my knowledge,the current shell $0 is- or the path tofirst nonoperand commandline argument the currently interpreting shell is fed when invoked. I previously stated that current processsh leader./somescript. So if your script is would path its executable$0 $ENV (as in variable to chmod +xsh) its pathbut this was incorrect because the shell is a new process of it' s invoked with a new $0,$ENV. but, if it is instead an argument to a shell

In this way ( like . ./myscript.sh, $(. ./myscriptsomescript.sh), or differs from sh. ./myscriptsomescript.sh), then because it runs in the current environment and $0 paths the shell process under which it runsis already set.

The above is incorrectYou can check this by comparing $0 to - I'm editing now/proc/$$/status.

echo 'script="/proc/$$/status"
    echo $0
    cat "$script"' \
    > ./script.sh
sh ./script.sh ; . ./script.sh

Thanks for the correction, @toxalot. I learned something.

In short, to the best of my knowledge, $0 is the path to the current process leader. So if your script is executable (as in chmod +x) its path is $0, but, if it is instead an argument to a shell ( like . ./myscript.sh, $(. ./myscript.sh), or sh ./myscript.sh), then $0 paths the shell process under which it runs.

The above is incorrect - I'm editing now.

My man page says:

$0 : expands to the name of shell or shell script.

It seems this translates to argv[0] of the current shell - or the first nonoperand commandline argument the currently interpreting shell is fed when invoked. I previously stated that sh ./somescript would path its $0 $ENV variable to sh but this was incorrect because the shell is a new process of it' s invoked with a new $ENV.

In this way sh ./somescript.sh differs from . ./somescript.sh because it runs in the current environment and $0 is already set.

You can check this by comparing $0 to /proc/$$/status.

echo 'script="/proc/$$/status"
    echo $0
    cat "$script"' \
    > ./script.sh
sh ./script.sh ; . ./script.sh

Thanks for the correction, @toxalot. I learned something.

added 47 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244

In short, to the best of my knowledge, $0 is the path to the current process leader. So if your script is executable (as in chmod +x) its path is $0, but, if it is instead an argument to a shell ( like . ./myscript.sh, $(. ./myscript.sh), or sh ./myscript.sh), then $0 paths the shell process under which it runs.

The above is incorrect - I'm editing now.

In short, to the best of my knowledge, $0 is the path to the current process leader. So if your script is executable (as in chmod +x) its path is $0, but, if it is instead an argument to a shell ( like . ./myscript.sh, $(. ./myscript.sh), or sh ./myscript.sh), then $0 paths the shell process under which it runs.

In short, to the best of my knowledge, $0 is the path to the current process leader. So if your script is executable (as in chmod +x) its path is $0, but, if it is instead an argument to a shell ( like . ./myscript.sh, $(. ./myscript.sh), or sh ./myscript.sh), then $0 paths the shell process under which it runs.

The above is incorrect - I'm editing now.

added 54 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
Loading
added 54 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
Loading
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
Loading