If you work with Bash regularly, you know that certain internal variables make the difference between a functional script and a robust one. At Q2BSTUDIO, as a software development and technology company, we build custom applications, integrate artificial intelligence, deploy cloud infrastructure on AWS and Azure, and audit systems with advanced cybersecurity. In all these projects, Bash scripts are the glue that automates processes and ensures consistency. That is why I want to share the seven variables I use in practically every script I write. Knowing them will help you write more reliable, debuggable, and professional code.
1. $0 – The script nameThis variable contains the path or name used to invoke the script. I use it to display custom usage messages (echo 'Usage: $0 [options]'), to log which script is running, and to reference the file itself in self-repair operations. In the automation environments we develop, $0 helps identify exactly which step failed in a deployment pipeline.
2. $? – Exit code of the last commandAfter executing any command, $? holds its exit status: 0 means success, any other value indicates an error. This variable is essential for flow control. In our custom software scripts, we validate each step with if [ $? -ne 0 ]; then to, for example, roll back cloud changes if an update fails. We also use it in AI agents that process data sequentially.
3. $# – Number of argumentsKnowing how many arguments the script received prevents silly errors. In a Business Intelligence data ingestion process, for instance, we require at least two parameters: the source and destination. if [ $# -lt 2 ]; then prints usage and exits. This simple validation saves time in production environments.
4. $@ – All arguments as separate wordsWhen you need to pass all arguments to another command while preserving quoting, $@ is the right choice (unlike $*). We use it in wrapper scripts that invoke cybersecurity tools like vulnerability scanners: nmap $@. This maintains flexibility without losing parameter integrity.
5. $EUID – Effective user IDThis variable shows the UID under which the process actually runs. If you launch a script with sudo, $EUID will be 0 (root), while $UID keeps the original user. I use it to check privileges before accessing protected system resources, like installing packages or modifying /etc. In cloud deployments with AWS or Azure, it is common to see if [ '$EUID' -ne 0 ]; then to force permission elevation.
6. $UID – Real user IDAlthough less used directly, $UID complements $EUID. When a script needs to know who invoked it (not who is effectively running it), $UID provides the answer. In auditing tools we develop for clients, we log both $UID and $EUID to trace identity in cybersecurity logs.
7. $BASH_VERSION – Bash versionNot all systems have the same Bash version. With $BASH_VERSION we can check if the interpreter supports modern features like associative arrays or advanced expansions. In AI projects that require manipulating large volumes of data, we ensure the version is 4.0 or higher. This pre-check prevents surprises in legacy environments.
These seven variables are the foundation of almost any well-written script. Mastering them allows you to build more secure automations, debug errors precisely, and maintain portable code. At Q2BSTUDIO we apply them daily in the development of custom applications, Power BI integrations, artificial intelligence agents, and cloud deployments. If you need a team that understands the importance of technical details, feel free to contact us.





