Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am wondering how to implement the following in a bash script.

if UID=1000, continue
else exit
confirm choice (y/n)
share|improve this question
up vote 3 down vote accepted
[ $UID -ne 1000 ] && exit     # exit if UID ist not equal 1000
share|improve this answer
This is a good solution too and works for the OP's question, but note that it does not work in sh (not relevant here but relevant in general). – Jason C Jul 13 '14 at 1:36
:) For all friends of sh and portable code: [ "$(id -u)" != 1000 ] && exit – Cyrus Jul 13 '14 at 2:55

You could do this:

if [ "$(id -u)" = "1000" ]; then
     do stuff for user 1000
     do stuff for other users

However, it would probably be more effective to chown the script to that user and then chmod 700, since it appears from your example that you are attempting to stop execution of the script for other users. This would also give you more control i.e. allowing you to use groups (w/ chmod 750 or whatever) to manage who can perform the action. If only part of your script should be accessible to user 1000 you can separate that part into its own script and apply permissions appropriately.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.