The workarounds for this all involve nasty extra parameters for different commands (e.g. the -print0 example)
The workarounds for this all involve nasty extra parameters for different commands (e.g. the -print0 example)
And that is a discouraged way to count files in shell script.
Still, simply adding -l to ls, could handle spaces correctly (the files count)
I insist, spaces are not your enemy, there are much more weird file names for a shell. Shell can handle spaces if used properly.
From where do your get that rule?
I write shell daily, and that are not my numbers regarding "spaces on filenames issues", sorry.
If it's because I did not explain how to do that, there is howto do it:
http://mywiki.wooledge.org/BashFAQ/004
If it's because the comment on the given example... $ touch file1
$ ls -l | wc -l
1
$ touch "file 2"
$ ls -l | wc -l
2
... (?)If we were talking about "new lines in file names", or "dashes at the beginning of file names", or code injection through file names, then we could be talking of more complex solutions.
But the space issues in shell are simple, and have known solutions. If you're a daily user or you're not at learning stage, spaces don't turn to be a issue.
A big problem, is that people that underestimate an unknown technology, does not take the time to learn properly that technology.
Many programmers think they "know" shell (and many beginners), so they don't invert more time and tests, and then they keep facing corner cases, facing known and documented and solved issues, etc...
You are asserting, than learning bash is harder than learning the corner cases of other more advanced languages. Is that what you say? do you think that "bash" has more corner cases than ... (?)
How we can patch that? With a web shell?
Totally true, I've miss totally the point.
For example, use null delimited values, because a variable (like IFS) cannot contain null bytes. Is this a workaround? I see it as pure logic.
Even understanding howto handle them, helps to understand the internal design of the shell and common external utilities.
Remembering is hard? not a "shell" specific issue... I use it daily and maybe that's why I don't face the same problems that other see so clear.
How about (for example):
$ for i in $(ls | grep 'cheese\|fish' | head -n 5) ...
I'm sure there is a way to make this work correctly (take 5 filenames containing either cheese or fish), but I'd need to put more thought into making it work correctly. ls | grep 'cheese\|fish' | head -n 5 | while read -r i … > touch {dark\ red,green,light\ blue}{cheese,fish\ fingers,fruit}
> ls -1 *(cheese|fish)*([1,5])
dark redcheese
dark redfish fingers
greencheese
greenfish fingers
light bluecheese
> for i in *(cheese|fish)*([1,5]) ...
Since that's shell globbing, it can cope with any spaces, newlines etc.I read the whole manpage some years ago, and noted down what seemed useful. I use "man zshexpn" when I forget the syntax for things like this.
'command1 | command2' just works in most circumstances, so it's frustrating that it falls apart when a filename with a space appears.
And it technically is a shell issue, insomuch as the shell is dividing up the ARGV for each program. The shell is perhaps not to blame, because it can't tell the difference between a filename that has a space in it, and ordinary output that just so happens to correspond with a filename. In other words, it's hard to see what a shell could do to make things better. But the problem still exists.
In that example we're not in front of a bash word splitting issue.
I agree with you that shell scripting has caveats one need to learn. As does Perl, C, PHP, Ruby, Node, Go, Java and what not.
I don't feel a big change is needed to handle spaces in shell scripts, my scripts handle them and I enjoy writing them. Maybe you know of minor tweaks for bash,zsh or any common shell which could be useful in general purpose of files with spaces in the name? don't hesitate to open them a bug, maybe we even get a fix.
But don't send them this example, and insist on it, because the conversation is over:
$ touch a_file
$ ls | wc
1 1 7
$ rm a_file
$ touch "a file"
$ ls | wc
1 2 7
Equivalent input, with/without spaces and expected output.The 2 is a word count, and we did pass two words, I don't expect a 1 there, _that_ could be a bug.
If you want a difficult example, then take a more real-world example: e.g. the workflow of a 'find [some stuff] |grep [some other stuff]' is one to consider. That's where horrid workarounds like -print0 and -z have to come in, but the simple 'find|grep' works fine up until a file has a space in it.
As I said, there's no simple fix, even for the re-organised form of 'grep [some other stuff] `find [some stuff]` because the shell can't tell the difference between a filename and just a stream of text in the output of one program.
while IFS= read -rd '' file; do
echo "do whatever with: $file"
grep whatever -- "$file"
done < <(find ~/whatevers -print0)
It's like natural language if you do it daily.Will handle not only spaces, but also
new
lines
on
file
names.
Have a nice day.
It's the canonical way of do it.
Other ways, even if they are "expected to work by inexperienced occasional users"... are simply flawed a first eye view.
A workaround is to ditch shell script, as soon as you face a problem, and blame shell script, and turn to do it in a "more advanced language" that has the same or more caveats. That could be a workaround.
Delimiting file names with null bytes, in case they could be split by any of the $IFS values, is NOT a workaround, is pure logic.
About half the package consists of evolutions of traditional Unix commands. The parts I use regularly are ksh and tw. tw ('tree walk') is sort of a 'find --exec' replacement with a C-like selector syntax. It's a bit verbose, so for interactive use I generally set up project-specific shell aliases with selection expressions, e.g.
alias cctw=$'tw -e "select: return (type == REG) && ((name == \'*.c\') || (name == \'*.h\') || (name == \'*.cpp\') || (name == \'*.cc\') || (name == \'*.h\') || (name == \'*.hpp\') || (name == \'*.mm\') || (name == \'*.inc\'));" '
and then use those, e.g. cctw egrep -w MyIdentifierIt also has nothing to do with the shell, nor does the general case of pipes you present below. Your shell redirects the output from ls to the input of wc. If you don't like the simple approach that tools consume and produce arbitrary text in a manner it sees fit for its purpose,, maybe it's your operating system that you have a beef with.