RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

How do I print all arguments submitted on a command line from a bash script?

Question:

How do I print all arguments submitted on a command line from a bash script?

Answer:

There are couple ways how to print bash arguments from a script. Try some scripts below to name just few.

In this first script example you just print all arguments:

 #!/bin/bash 
echo $@ 

If you intend to do something with your arguments within a script you can try somethign simple as the following script:

 #!/bin/bash 
 for i; do 
echo $i done

Next we have some script which are doing the same as the previous bash script but employ different approach:

 #/bin/bash 
 for i in $*; do 
   echo $i 
 done

Let's print all bash arguments using shift:

 #!/bin/bash 
 
while (( "$#" )); do 
  echo $1 
  shift 
done 
 

Or we can do something obscure like this to print all bash arguments:

 #/bin/bash 
 
# store arguments in a special array 
args=("$@") 
# get number of elements 
ELEMENTS=${#args[@]} 
 
# echo each element in array  
# for loop 
for (( i=0;i<$ELEMENTS;i++)); do 
    echo ${args[${i}]} 
done 

Linux questions and answers

Share this linux post:

Submit How do I print all arguments submitted on a command line from a bash script? in Delicious Submit How do I print all arguments submitted on a command line from a bash script? in Digg Submit How do I print all arguments submitted on a command line from a bash script? in FaceBook Submit How do I print all arguments submitted on a command line from a bash script? in Google Bookmarks Submit How do I print all arguments submitted on a command line from a bash script? in Stumbleupon Submit How do I print all arguments submitted on a command line from a bash script? in Technorati Submit How do I print all arguments submitted on a command line from a bash script? in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download