Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6030839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:05:48+00:00 2026-05-23T05:05:48+00:00

I am trying to write a bash script that will examine the first argument,

  • 0

I am trying to write a bash script that will examine the first argument, and if its one of particular values then it will remove that argument from the arguments and set some stuff up:

#!/bin/bash -x

echo args=$@

if [ "$1" == "valgrind" ]; then
    echo running under valgrind
    set $tool=valgrind
    shift 1
elif [ "$1" == "debug" ]; then
    echo running under gdb
    set $tool=gdb --args
    shift 1
else
    echo running normally
    set $tool=""
fi

echo tool=$tool
echo args=$@

However, When I run this it consumes all the arguments! E.g.:

./my_script valgrind --something=y -r

+ echo args=valgrind --something=y -r
args=valgrind --something=y -r
+ '[' valgrind == valgrind ']'
+ echo running under valgrind
running under valgrind
+ set =valgrind
+ shift 1
+ echo tool=
tool=
+ echo args=
args=

and if the if-statements aren’t followed:

./my_script valgrindx --something=y -r

+ echo args=valgrindx --something=y -r
args=valgrindx --something=y -r
+ '[' valgrindx == valgrind ']'
+ '[' valgrindx == debug ']'
+ echo running normally
running normally
+ set =
+ echo tool=
tool=
+ echo args==
args==

What is happening, and how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:05:49+00:00Added an answer on May 23, 2026 at 5:05 am

    It’s because set blah blah blah actually sets the $1 ... $N parameters to whatever the arguments evaluate to, as shown in the following transcript:

    pax$ echo $1
    
    pax$ set $tool=hello ; echo $1
    =hello
    
    pax$ tool=xyzzy ; set $tool=hello ; echo $1
    xyzzy=hello
    

    (in that second command, $tool is not set which is why it comes out as blank).

    In your case, set $tool=valgrind is setting $1 to whatever the string $tool=valgrind evaluates to, which is why $@ is changing. It is not setting the environment variable tool to valgrind.

    If you want to set the variable, just use:

    tool=valgrind
    

    or, to make it available to subshells:

    export tool=valgrind
    

    The set command is used to either display environment stuff or set shell attributes and arguments, not named variables. If you have a look at the man bash_builtins manpage, you’ll see the bit that explains this behaviour (search for the description of set):

    Any arguments remaining after the options are processed are treated as values for the positional parameters and are assigned, in order, to $1, $2, ... $n.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write my first semi advanced bash script that will take input
I'm trying to write a bash script that will process a list of files
I am trying to write a bash or PHP script that will execute a
I'm trying to write a bash script that will let me download multiple web
I am trying to write a simple bash script that will search for files
I'm trying to write a bash script that takes a few variables and then
I'm trying to write a Bash script that will SSH into a machine and
I am trying to write a simple bash script that will copy the entire
i'm trying to write a SVN pre-commit hook script in Linux Bash that will
I'm trying to write a bash script that will recursively search a directory, find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.