HappiomHappiom
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
Expand All Collapse All
  • Linux Examples
    • 10 Crontab Commands and Examples to Automate Tasks in Linux
    • 10 Linux Commands to Manage Users and Permissions Effectively
    • 10 Practical find Command Examples Every Linux User Should Know
    • 10 Linux Commands to Monitor System Performance - CPU, RAM, Disk, etc.
    • 10 Linux Archive and Compression Commands
    • 10 Disk Usage Commands to Find and Clean Up Space in Linux
    • 10 grep Command Examples to Supercharge Your Searches
    • 10 Networking Commands in Linux for Troubleshooting and Monitoring
    • 10 File Management Commands in Linux for Daily Use
    • 10 Linux Commands to Kill, Pause, and Manage Processes
    • 10 Linux Commands to Search Files Recursively Like a Pro
    • 10 Essential Linux Commands for Searching Files
    • 5 Quick Linux Commands to Find Disk Space
    • Difference between $? vs $@ in Linux Shell Scripting

Difference between $? vs $@ in Linux Shell Scripting

In a shell script, `x=$?` is used to capture the exit status of the last executed command into the variable `x`.

Let’s see a quick rundown of how it works:

  • `$?` is a special variable in shell scripting that holds the exit status of the most recently executed command.

Exit status is a number that a command returns to indicate whether it succeeded or failed. Typically, an exit status of `0` means success, while any non-zero value indicates an error.

If you use `x=$?` immediately after a command, `x` will store the exit status of that command. For example:

#!/bin/sh

# Run a command
ls /some/directory

# Capture the exit status of the 'ls' command
x=$?

# Check if the 'ls' command was successful
if [ $x -eq 0 ]; then
echo "The command succeeded."
else
echo "The command failed with exit status $x."
fi

In this script:

  1. `ls /some/directory` attempts to list the contents of a directory.
  2. `x=$?` captures the exit status of the `ls` command.
  3. The `if` statement checks if the exit status (`x`) is `0` (indicating success) and prints a corresponding message.

This practice is useful for error handling and controlling the flow of your script based on the success or failure of commands.

In shell scripting, `$@` is a special variable that represents all the positional parameters passed to a script or function. Each parameter is treated as a separate argument.

Let’s see a breakdown of how `$@` works:

Usage in Scripts

1. Script Arguments

When you pass arguments to a script, `$@` contains all those arguments, preserving their individual integrity. For example:

#!/bin/sh

echo "All arguments using \$@:"
for arg in "$@"; do
echo "$arg"
done

If you run this script with the command:

./script.sh arg1 "arg2 with spaces" arg3

The output will be:

All arguments using $@:
arg1
arg2 with spaces
arg3

Note how `”$@”` ensures that arguments with spaces are handled correctly.

2. Function Arguments

Similarly, `$@` can be used inside functions to access all arguments passed to the function:

my_function() {
echo "Function arguments:"
for arg in "$@"; do
echo "$arg"
done
}

my_function "arg1" "arg2 with spaces" "arg3"

Output:

Function arguments:
arg1
arg2 with spaces
arg3

Differences Between `$@` and `$*`

  • `”$@”` When quoted, it expands to each argument as a separate quoted string, preserving spaces within arguments. This is particularly useful when you want to preserve the integrity of arguments, especially those containing spaces.
  • `$*` When quoted, it expands to a single string with arguments separated by the first character of the `IFS` (Internal Field Separator) variable, which is usually a space. This can cause issues if any arguments contain spaces.

echo “$*”

This will output all arguments as a single string separated by spaces (or another separator depending on the `IFS` setting).

In short, `$@` is often preferred over `$*` because it preserves the integrity of each argument when quoted, which is crucial for handling arguments with spaces or special characters correctly.

Related Articles
  • 10 Crontab Commands and Examples to Automate Tasks in Linux
  • 10 Linux Commands to Manage Users and Permissions Effectively
  • 10 Practical find Command Examples Every Linux User Should Know
  • 10 Linux Commands to Monitor System Performance – CPU, RAM, Disk, etc.
  • 10 Linux Archive and Compression Commands
  • 10 Disk Usage Commands to Find and Clean Up Space in Linux

No luck finding what you need? Contact Us

Previously
5 Quick Linux Commands to Find Disk Space
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2025 Happiom. All Rights Reserved.