a problem with bash aliases and cmdline arguments

by @jehiah on 2007-05-03 17:34UTC
Filed under: All , Linux , SysAdmin

So i have a problem with passing command line arguments to a bash alias which expands to execute more than one command.

I’m not lazy but i haven’t found the answer yet, so i hope someone smarter than me can shed some light.

In bash you can alias things to be expanded later on. Many commands are often aliased like

alias ls='ls --color=tty'

So when you type ls mydir on the command line, bash interprets that with the alias and executes ls --color=tty mydir

Simple so far right?

You can also alias so that you run more than one command

alias ls="cd $HOME && ls --color=tty"

so it changes to your home directory then runs the ls command regardless of where you are; usefull? not usefull? who cares; thats not my problem

My problem comes where the first command is a shell script which needs some of the command line arguments. I can’t find a way to pass on the command line arguments to the first part of the command

alias m="my_script.sh && . $HOME/.bashrc"

So i’m running my script (we’ll assume it’s in the path) and then i’m reloading the bash profile (thats what the period is for).

How do i pass command line arguments to my_script.sh? When the command is expanded, the extra command line arguments get passed to the sourcing of the bashrc

if you try adding in $* or $1 to get bash to pass along the command line arguments they get interpreted when creating the alias instead of during the execution of the script. (escaping the $ doesn’t work either)

You can’t redirect stdin to the bash script because the command line arguments are already passed along to bash, and are not waiting to be read by stdin anymore

any other ideas?

Subscribe via RSS ı Email
© 2022 - Jehiah Czebotar