Re: Tests [ 0 -eq $UID ] don't work as in bash

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2016-09-07 11:55:00 -0400, Paul Smith:
> On Wed, 2016-09-07 at 16:44 +0200, Andrey Voropaev wrote:
> > if [ 0 -eq $UID ]
> 
> The variable UID is not defined to be automatically set by the shell in
> POSIX; having it set is a bash extension.  dash doesn't set it
> automatically for you.
> 
> You'll have to set it yourself:
> 
>   UID=$(id -u)
> 
> Also you should quote variable references, in general:
> 
>   if [ 0 -eq "$UID" ]
> 
> Be sure that if your scripts rely on bash-isms you start them with
> #!/bin/bash and if you don't want them to rely on bash-isms, you start
> them with #!/bin/sh.
[...]

Note that both zsh and bash have it, both from the start, both
copied from tcsh ($uid in tcsh).

In zsh, you can also set UID (and USERNAME and EUID, and GID...)
to change your uid (assuming you have the permission to do so).

$ sudo zsh -c 'id; UID=1000; id; UID=0'
uid=0(root) gid=0(root) groups=0(root)
uid=1000(chazelas) gid=0(root) groups=0(root)
zsh:1: failed to change user ID: operation not permitted

In bash, UID is read-only.

-- 
Stephane
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux