Passing arguments to alias with multiple commands


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Passing arguments to alias with multiple commands
# 1  
Old 05-20-2011
Passing arguments to alias with multiple commands

I have a few aliases set up on AIX servers in my .kshrc file. Some of them contain multiple commands that are piped together.

A simple example would be something like this:
Code:
 
# alias to list directory contents as root and sort by size.
alias lss='sudo ls -l | sort -nbk5'

When I call the alias from the command line, is it possible to pass a command line switch to one of the commands in the sequence using the alias? For instance, with the above example, if I wanted to include a "-a" argument to the ls command in addition to the -l that is part of my alias, could I do that?

I know I could create multiple aliases with the various argument permutations, but if there is a way to pass the arguments in, it would mean fewer aliases to have to create (and remember.)

Thank you.
# 2  
Old 05-20-2011
Code:
alias lss='sudo ls -l $@ | sort -nbk5'
lss -a

# 3  
Old 05-20-2011
Quote:
Originally Posted by vgersh99
Code:
alias lss='sudo ls -l $@ | sort -nbk5'
lss -a

Hmm, as fas as I remember `aliases' can't handle arguments, since they are inserted literally. If I need arguments I wrap the code into a function. At least the following example with alias didn't work in Bash and KSH:
Code:
sidorenko@sidorenko>
>alias myecho='echo -- $@ --'

sidorenko@sidorenko>
0 ~>myecho Duck
-- -- Duck

This User Gave Thanks to sidorenko For This Post:
# 4  
Old 05-20-2011
worked fine with Solaris' bash:
Code:
alias foo='ls $@'
foo -l

# 5  
Old 05-20-2011
Try please my example with `echo', I want to make sure that it was `$@' which expanded to `-l'. And not that `foo' was expanded to `ls ' and the `-l' was just appended to the expanded alias.
# 6  
Old 05-20-2011
Thank you for the quick replies.

The suggestion by vgersh99 doesn't appear to work (at least not in my environment which is KSH88 on AIX). When I execute the alias with an argument it tries to pass the argument to the last command in the sequence. So in this example it attempts to add a "-a" to the sort command, not to the ls command.

sidorenko -- good idea to wrap the command in a function instead of using an alias. That seems like a promising way to do what I want to do. I'll give it a try.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh multiple hops to execute commands with arguments

Hi I need to write a script to ssh through several hops (e.g. HostA-HostB-HostC-HostD), where Host A does not have direct assess to HostC ; HostB cannot access HostD directly. when I ssh 3 hops and run command with arg1, arg2 and redirect the output to a file, e.g. HostA> ssh -t HostB ssh -t... (3 Replies)
Discussion started by: chiensh
3 Replies

2. Shell Programming and Scripting

Passing multiple arguments

Hi, I know with getopts you can pass arguments from the command line ./script -ab -c apple But it doesn't support 2 or more arguments for ONE option. Is there any other way to do this? Thanks (2 Replies)
Discussion started by: testa500
2 Replies

3. Shell Programming and Scripting

Passing multiple arguments to a shell script

Hi Gurus, Need some help with the shell scripting here. #!/bin/ksh ps -ef | grep -i sample.ksh | grep -v grep > abc.txt if then echo "sample.ksh is executing" else echo "sample.ksh is not executing" fi (1 Reply)
Discussion started by: jayadanabalan
1 Replies

4. Shell Programming and Scripting

Passing multiple run time arguments

the scenario is - If I pass 3 three arguments( run time) , it should list all .txt files from a path to temp file if I pass 2 arguments ( run time) , it should list all .csv files from the same path to another temp file the above scenario should be handled in single code and also I dont know ... (2 Replies)
Discussion started by: Prashanth B
2 Replies

5. UNIX for Dummies Questions & Answers

Create alias files (not alias commands)

If one: $ find -name 'some expression' -type f > newfile and then subsequently wants to create an alias file from each pathname the find command retrieved and the > placed within 'newfile', how would one do this? Ideally, the newly created alias files would all be in one directory. I am... (3 Replies)
Discussion started by: Alexander4444
3 Replies

6. Shell Programming and Scripting

Help required in passing multiple arguments from a shell script to a pl/sql block

Hi, hope everyone are fine. Please find my issue below, and I request your help in the same In a configuration file, i have a variable defined as below TEST = 'One','Two','Three' I am trying to pass this variable in to a sql script which is define in a pl/sql block as follows, In the... (1 Reply)
Discussion started by: ramakanth_burra
1 Replies

7. UNIX for Dummies Questions & Answers

How to make delays between multiple commands in an alias (ircII)?

Okay so I have an alias that looks like this: ALIAS gscn { MSG gscn Test1 MSG gscn Test2 MSG gscn Test3 MSG gscn Test4 MSG gscn Test5 } How do I make it wait 5 seconds between each command before it executes the next one after that in order from top to bottom? I tried the TIMER... (1 Reply)
Discussion started by: guitarscn
1 Replies

8. Answers to Frequently Asked Questions

Passing variables/arguments/parameters to commands

A good place to start is simple variable passing.... Passing variables from one script to another The next level is passing a variable into a more complex command such as using a variable in a sed command. There are some simple quoting techniques that are very general. These are mentioned... (0 Replies)
Discussion started by: Perderabo
0 Replies

9. UNIX for Dummies Questions & Answers

Alias confusion - multiple commands ?

I've read the man on the alias command, and I am perplexed. I don't see a way to create an alias that will perform more than one command......is there a way to create an alias that is a string of a commands ? (1 Reply)
Discussion started by: HikerLT
1 Replies

10. UNIX for Dummies Questions & Answers

Passing arguments to an alias

I want to have an alias for the command fold -78 filename | lp How do I set my alias so that the argument passed is filename ?? alias lp='fold -78 | lp' then lp filename wont work cuase this is fold -78 | lp filename (1 Reply)
Discussion started by: pmcg
1 Replies
Login or Register to Ask a Question