Skip to main content
grammar tweaks
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

If you're trying to iterate over files in a directory you need to glob the directory like so:

#!/bin/bash

List () {
    for item in "${1}/"*
            do
            echo "$item"
    done
}

Then call it like:

$ list ~

Alternatively, if you want to pass multiple files as arguments you can write your forfor loop like this:

List () {
    for item
            do
            echo "$item"
    done
}

Which can then be called as:

$ list ~/*

WhatsWhat's wrong with your current function:

When you call it with a glob, it passes each file in the directory as a separate argument. LetsLet's say your home directory contains file1, file2, and file3. When you call list ~/*, you are essentially calling:

list ~/file1 ~/file2 ~/file3

Then your for loop is only being passed positional parameter 1 so for item in ~/file1 and the other positional parameters are unused.

Also thanks Ilkkachu for pointing out that you also forgot a / in your hashbang, which I completely missed as well...

If you're trying to iterate over files in a directory you need to glob the directory like so:

#!/bin/bash

List () {
    for item in "${1}/"*
            do
            echo "$item"
    done
}

Then call it like:

$ list ~

Alternatively if you want to pass multiple files as arguments you can write your for loop like this:

List () {
    for item
            do
            echo "$item"
    done
}

Which can then be called as:

$ list ~/*

Whats wrong with your current function:

When you call it with a glob it passes each file in the directory as a separate argument. Lets say your home directory contains file1, file2, and file3. When you call list ~/* you are essentially calling:

list ~/file1 ~/file2 ~/file3

Then your for loop is only being passed positional parameter 1 so for item in ~/file1 and the other positional parameters are unused.

Also thanks Ilkkachu for pointing out that you also forgot a / in your hashbang which I completely missed as well...

If you're trying to iterate over files in a directory you need to glob the directory like so:

#!/bin/bash

List () {
    for item in "${1}/"*
            do
            echo "$item"
    done
}

Then call it like:

$ list ~

Alternatively, if you want to pass multiple files as arguments you can write your for loop like this:

List () {
    for item
            do
            echo "$item"
    done
}

Which can then be called as:

$ list ~/*

What's wrong with your current function:

When you call it with a glob, it passes each file in the directory as a separate argument. Let's say your home directory contains file1, file2, and file3. When you call list ~/*, you are essentially calling:

list ~/file1 ~/file2 ~/file3

Then your for loop is only being passed positional parameter 1 so for item in ~/file1 and the other positional parameters are unused.

Also thanks Ilkkachu for pointing out that you also forgot a / in your hashbang, which I completely missed.

added 575 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163

If you're trying to iterate over files in a directory you need to glob the directory like so:

#!/bin/bash

List () {
    for item in "${1}/"*
            do
            echo "$item"
    done
}

Then call it like:

$ list ~

Alternatively if you want to pass multiple files as arguments you can write your for loop like this:

List () {
    for item
            do
            echo "$item"
    done
}

Which can then be called as:

$ list ~/*

Whats wrong with your current function:

When you call it with a glob it passes each file in the directory as a separate argument. Lets say your home directory contains file1, file2, and file3. When you call list ~/* you are essentially calling:

list ~/file1 ~/file2 ~/file3

Then your for loop is only being passed positional parameter 1 so for item in ~/file1 and the other positional parameters are unused.

Also thanks Ilkkachu for pointing out that you also forgot a / in your hashbang which I completely missed as well...

If you're trying to iterate over files in a directory you need to glob the directory like so:

#!/bin/bash

List () {
    for item in "${1}/"*
            do
            echo "$item"
    done
}

Then call it like:

$ list ~

Alternatively if you want to pass multiple files as arguments you can write your for loop like this:

List () {
    for item
            do
            echo "$item"
    done
}

Which can then be called as:

$ list ~/*

If you're trying to iterate over files in a directory you need to glob the directory like so:

#!/bin/bash

List () {
    for item in "${1}/"*
            do
            echo "$item"
    done
}

Then call it like:

$ list ~

Alternatively if you want to pass multiple files as arguments you can write your for loop like this:

List () {
    for item
            do
            echo "$item"
    done
}

Which can then be called as:

$ list ~/*

Whats wrong with your current function:

When you call it with a glob it passes each file in the directory as a separate argument. Lets say your home directory contains file1, file2, and file3. When you call list ~/* you are essentially calling:

list ~/file1 ~/file2 ~/file3

Then your for loop is only being passed positional parameter 1 so for item in ~/file1 and the other positional parameters are unused.

Also thanks Ilkkachu for pointing out that you also forgot a / in your hashbang which I completely missed as well...

added 1 character in body
Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441

If you're trying to iterate over files in a directory you need to glob the directory like so:

#!/bin/bash

List () {
    for item in "${1}/"*
            do
            echo "$item"
    done
}

Then call it like:

$ list ~

Alternatively if you want to pass multiple files as arguments you can write your for loop like this:

List () {
    for item
            do
            echo "$item"
    done
}

Which can then be called as:

$ list ~/*

If you're trying to iterate over files in a directory you need to glob the directory like so:

#!bin/bash

List () {
    for item in "${1}/"*
            do
            echo "$item"
    done
}

Then call it like:

$ list ~

Alternatively if you want to pass multiple files as arguments you can write your for loop like this:

List () {
    for item
            do
            echo "$item"
    done
}

Which can then be called as:

$ list ~/*

If you're trying to iterate over files in a directory you need to glob the directory like so:

#!/bin/bash

List () {
    for item in "${1}/"*
            do
            echo "$item"
    done
}

Then call it like:

$ list ~

Alternatively if you want to pass multiple files as arguments you can write your for loop like this:

List () {
    for item
            do
            echo "$item"
    done
}

Which can then be called as:

$ list ~/*
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163
Loading