• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Check if the script is run by root at the start of the script

by admin

Check root at start of script

Some scripts need to be run as root and you may want to check at the start of that script that it is running as root. This can be done by checking the environment variable $EUID. This variable will hold the value 0 if it’s being run as root.

#!/bin/bash
if [ "$EUID" -ne 0 ];then
    echo "Please run this script as root"
    exit 1
fi

Filed Under: DevOps, Shell Scripting

Some more articles you might also be interested in …

  1. How to write Bubble sort in Bash
  2. ansible-galaxy – Create and manage Ansible roles (Command Examples)
  3. “docker image” Command Examples
  4. How to use “break” and “continue” statements in shell scripts
  5. aws-shell: command not found
  6. Troubleshooting kubectl Error: The connection to the server x.x.x.x:6443 was refused – did you specify the right host or port?
  7. “docker save” Command Examples
  8. How to create Docker Image from a Container and Dockerfile
  9. Install AzCopy on Linux – Fastest way to copy in Azure
  10. Run Docker as a non-root user

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright