Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "$1/*" mean in "for file in $1/*"

Tags:

linux

bash

shell

The short bash script below list all files and dirs in given directory and its sub. What does the $1/* mean in the script? Please give me some references about it. Thanks

#!/bin/sh

list_alldir(){
    for file in $1/*
    do
        if [ -d $file ]; then
            echo $file
            list_alldir $file
        else
            echo $file
        fi
    done
}   

if [ $# -gt 0 ]; then 
    list_alldir "$1"
else
    list_alldir "."
fi
like image 325
louxiu Avatar asked Jan 08 '12 13:01

louxiu


People also ask

What is $1 in shell script?

$1 - The first argument sent to the script. $2 - The second argument sent to the script. $3 - The third argument... and so forth. $# - The number of arguments provided. $@ - A list of all arguments provided.

What does $# mean in script?

$# is the number of positional parameters passed to the script, shell, or shell function. This is because, while a shell function is running, the positional parameters are temporarily replaced with the arguments to the function. This lets functions accept and use their own positional parameters.

What is $? $# $*?

$# Stores the number of command-line arguments that were passed to the shell program. $? Stores the exit value of the last command that was executed. $0 Stores the first word of the entered command (the name of the shell program). $* Stores all the arguments that were entered on the command line ($1 $2 ...).

What is the meaning of $1 in Unix?

Linux / Unix Community $1 is the first commandline argument. If you run ./asdf.sh a b c d e, then $1 will be a, $2 will be b, etc. In shells with functions, $1 may serve as the first function parameter, and so forth.


2 Answers

It's the glob of the first argument considered as a directory

In bash scripts the arguments to a file are passed into the script as $0 ( which is the script name ), then $1, $2, $3 ... To access all of them you either use their label or you use one of the group constructs. For group constructs there are $* and $@. ($* considers all of the arguments as one block where as $@ considers them delimited by $IFS)

like image 137
zellio Avatar answered Oct 06 '22 19:10

zellio


$1 means the first parameter.
for file in $1/* means loop with the variable file having the value of the name of each file in the directory named in the first parameter.

like image 21
Bohemian Avatar answered Oct 06 '22 18:10

Bohemian