Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-29-2011, 06:22 PM   #1
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
BASH: 'ls' alias gives the opposite result.

I've tried it as an alias (the 'x' is for 'except')
alias lx='ls --ignore='$1''

alias lx='ls --ignore=*$1'

alias lx='ls --ignore="*\$@"'

alias lx='ls --ignore="*$1"'
and as a function with this and other details
function lx() { 
ls --ignore='$1' 
When I type in a command such as
lx *.jpg
in a folder that contains more JPEG files than files of other types, I get a stdout that's no different than if I'd entered ls *jpg.

This is not what I'm looking to make a shortcut for.

What could I be doing wrong?

Old 10-29-2011, 11:27 PM   #2
Senior Member
Registered: Aug 2009
Distribution: CentOS
Posts: 4,308

Rep: Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962
Your problem is that an alias is not a shell function or program invocation, and does not perform parameter substitution. Your "*.jpg" argument gets expanded by the shell and appended to the command line after the alias substitution is done. You can get what you want if you write the alias definition and invocation as
alias lx="ls --ignore"
lx \*.jpg
which will become
ls --ignore \*.jpg
Note that the "*" is escaped so that it gets passed literally to the 'ls' command, and the "--ignore" flag and its argument are passed as two separate arguments rather than in a single argument with an "=" separator.

When you are pushing against the limits of what you can do with "alias", you are better off coding what you want as a simple shell function. You could get the behavior you wanted by adding this to your .bashrc file
function lx() { ls --ignore=$1; }
You do still have to escape any shell meta-characters in the ignore pattern. You can generally get away without that when invoking the 'ls' command directly because the pattern "ignore=*.jpg" is unlikely to match any files in the current directory (unless you are in the habit of creating files with names that begin with "ignore=").
1 members found this post helpful.
Old 10-30-2011, 02:33 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,720

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
You can also use single quotes instead of escaping.
1 members found this post helpful.
Old 11-01-2011, 05:47 PM   #4
Registered: May 2009
Posts: 181

Original Poster
Blog Entries: 9

Rep: Reputation: 15
I tried your suggestion, and added lx as a function to my .bashrc. It works.

I guess I was hoping to create an alias, or command shortcut of some kind, that didn't require any special treatment of the pattern to be hidden or ignored. That would probably take a whole script (and probably not in bash) or something more sophisticated, like writing and compiling a variation of ls that had something similar to grep's "-v" option built into it.

I also played around a bit with the "--hide" option of ls, after I made the first post to this thread, but as the results were just about the same -- and now I know the reasons why -- I figured I'd just wait for replies here before continuing.

Thanks for the help so far, folks.

Old 11-03-2011, 05:08 AM   #5
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
function lx() { 
ls --ignore='$1' 

$ lx *.jpg
This fails for two reasons. First, you hard-quoted the variable, so ls sees only the literal pattern '$1'. You need to use double-quotes to let the variable expand.

Second, the shell expands globbing patterns before passing them to the command. so your function (after fixing the above) would actually execute as:

ls --ignore="file1.jpg file2.jpg file3.jpg"
You have to quote or escape the globbing pattern on the command line so that it gets passed literally.

( By the way, you should use either function lx, or lx(), but not both together. Bash ignores the error, but it's still bad syntax. Posix specifies funcname() only. Also, the ; at the end is only needed to separate the closing bracket from a previous command on the same line. When the bracket is on a line of its own, the semicolon before it causes a syntax error. )

lx() { 
	ls --ignore="$1" 

$ lx "*.jpg"
Another possibility would be to build the globbing pattern into the function itself.

lx() { 
	ls --ignore="*$1*" 

$ lx .jpg
Note that the first option is more flexible, as it allows you to pass any globbing pattern to the ignore list, whereas the second one will only work reliably for literal text patterns. Also note that the function as written will only accept a single argument, and that you can't pass any other options to it.

A more robust function would look something like this:

lx() {
	local x="$1" ; shift
	ls --ignore="*$x*" "$@"
This allows other options to be passed to the command too. Note though that the ignored pattern must still always be the first argument given.

It might do you better in the long run to just learn how to use globs more effectively

Last edited by David the H.; 11-03-2011 at 05:14 AM. Reason: made x local, plus added last comment


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash -D (or bash --dump-strings) unexpected result Ubx Linux - General 2 02-24-2011 07:41 PM
[SOLVED] grep result for if clause (bash) CRUDE Programming 3 10-31-2009 12:51 PM
bash result to PHP eco Programming 3 05-18-2009 07:50 PM
[bash] how to put nslookup result next to ip in file kriezo Programming 5 02-25-2008 01:48 AM
BASH Command Result redhatnoob Programming 2 08-19-2004 07:25 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration