Monday, February 26, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 584  / 3 Years ago, tue, july 20, 2021, 8:17:19

When I run the following command



./command *


using this script



#!/bin/bash

for f in ./$1
do
echo $f
done


only the first file specified in $1 is echoed. My question is why?


More From » bash

 Answers
1

Because you didn't used simple or double quotes when you ran your command:





./command '*'


or:



./command "*"


Or, if you want certainly to use ./command *, then make the following modification in your script:



#!/bin/bash

for f in ./"$@"
do
echo $f
done


That's because $1 refers to the first argument from your command ans $@ refers to all arguments from your command.



Read also some documentation in this sense: http://tldp.org/LDP/abs/html/internalvariables.html#APPREF


[#28460] Wednesday, July 21, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bblerest

Total Points: 240
Total Questions: 119
Total Answers: 113

Location: Wallis and Futuna
Member since Mon, May 18, 2020
4 Years ago
;