Hacker News new | past | comments | ask | show | jobs | submit login

> The amount of shell and build scripts on Unix-likes that die horrible deaths when encountering spaces is not funny.

That's so accurate. I used to write shell scripts with

    command $1
until I got a few too many nasty surprises with dashes in filenames.



Always use "$@"

    foo() {
        for arg in "$@" ; do
            echo "arg is \"${arg}\""
        done
    }

    foo 'bar baz' 'spaces in filename.txt'
> dashes in filenames.

When writing shell scripts it's a good idea to use the -- option whenever possible

    stupid_backup() {
        cp -a -- "$@" /stupid/backup/dir/
    }
 
    # copies 2 files
    stupid_backup --files '-with -leading -dashes'


What does the -- option do? I haven't encountered that before.


It means "everything past here should not be parsed as a - or -- flag". If you have a file named "-l", then ls -- -l will show you that file, instead of doing a long listing.


It should be noted that not all flag parsers support it. But most people use getopt so it's not a big deal.


Thanks- that's really useful.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: