InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

Bash – pass all arguments from one script to another

By admin on Dec 9, 2015

When writing a wrapper bash script, we often need to pass all arguments passed to the wrapper scrip to another script. Here is quick bash code snippet to pass all arguments to another script:

Passing all arguments in bash using $@

Here is sample code to print whatever arguments are passed to it.

#!/bin/bash
echo "Total $# arguments passed to me are: $*"

The code which passes argument using $@

#!/bin/bash
./child.sh $@

To test above bash script we can run this on command line:

./pass_all.sh a b c

And here is the outcome:

Total 3 arguments passed to me are: a b c
Env: GNU bash, version 4.2.46

Passing arguments after removing first arg

In case you want to consume one argument before passing to second script, then you can use shift as shown below:

#!/bin/bash
#remove first argument
FIRST=$1
shift
echo "Removed first arg: $FIRST"
./child.sh $@

To test above bash script we can run this code on command line:

./pass_all_after_shift.sh a b c

And here is the outcome:

Removed first arg: a
Total 2 arguments passed to me are: b c
Env: GNU bash, version 4.2.46

Additional notes on $@ and $*

  1. Note that $@ can also be used as "$@"
  2. An alternate approach is to use $*. It does not work when used within quotes (“$*”) though. So its always better to use $@.

Suggested posts:

  1. How to display auto fade out message using jQuery
  2. Can global variables be modified in bash function?
  3. Css position property – static, relative, absolute and fixed positioning
  4. Bash – append text to a variable
  5. Linux – sending mail from command using mailutils
  6. Bash shell – check if file or directory exists
  7. jQuery – get javascript object
  8. Bash – newline and other escape character in string
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Linux, Mac, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress