Bash: How to Pass in Arguments to an Alias: Cannot Use a Function - Syntax of Bash Conditionals

Make a Bash alias that takes a parameter?

Bash alias does not directly accept parameters. You will have to create a function.

alias does not accept parameters but a function can be called just like an alias. For example:

myfunction() {
#do things with parameters like $1 such as
mv "$1" "$1.bak"
cp "$2" "$1"
}

myfunction old.conf new.conf #calls `myfunction`

By the way, Bash functions defined in your .bashrc and other files are available as commands within your shell. So for instance you can call the earlier function like this

$ myfunction original.conf my.conf

How to pass command line arguments to a shell alias?

You found the way: create a function instead of an alias. The C shell has a mechanism for doing arguments to aliases, but bash and the Korn shell don't, because the function mechanism is more flexible and offers the same capability.

ZSH alias with parameter

You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the [].

gitall() {
git add .
if [ "$1" != "" ] # or better, if [ -n "$1" ]
then
git commit -m "$1"
else
git commit -m update
fi
git push
}

*: Most shells don't allow arguments in aliases, I believe csh and derivatives do, but you shouldn't be using them anyway.

alias maybe shouldn't be an alias?

Use a function, not an alias:

edit() {
emacs "$@" &
}

Using alias in the middle of bash command

You can use a function like this:

function myname() {
"$@" -n mynamespace
}

The "$@" means "insert all arguments here". The double-quotes ensures that they are word-split correctly. Then you'd invoke it using

myname kubectl get pods

Syntax is a bit different but the behavior is the same. You could probably work something else out if you really wanted to use the syntax you want, but it would be more complicated.

Probably simpler would be to define a variable like this:

export myname="-n mynamespace"

and execute it using

kubectl get pods $myname

Passing parameters to a Bash function

There are two typical ways of declaring a function. I prefer the second approach.

function function_name {
command...
}

or

function_name () {
command...
}

To call a function with arguments:

function_name "$arg1" "$arg2"

The function refers to passed arguments by their position (not by name), that is $1, $2, and so forth. $0 is the name of the script itself.

Example:

function_name () {
echo "Parameter #1 is $1"
}

Also, you need to call your function after it is declared.

#!/usr/bin/env sh

foo 1 # this will fail because foo has not been declared yet.

foo() {
echo "Parameter #1 is $1"
}

foo 2 # this will work.

Output:

./myScript.sh: line 2: foo: command not found
Parameter #1 is 2

Reference: Advanced Bash-Scripting Guide.

How to let an alias propagate to another shell script

After going into Bash Manual and google, export -f function_name is what I want.

Thank Etan Reisner for the advice about function being recursive and a demo code as follow:

#!/bin/bash --posix
# Avoid an error about function xx() statement on some os, e.g. on ubuntu, Syntax error: "(" unexpected
# http://ubuntuforums.org/archive/index.php/t-499045.html

#########################################################################################################
# << A example about exporting a function >>
# hook ln and propagate it to other scripts to pollute the environment of subsequently executed commands
#########################################################################################################

## https://stackoverflow.com/questions/3648819/how-to-make-symbolic-link-with-cygwin-in-windows-7
## https://cygwin.com/cygwin-ug-net/using-cygwinenv.html
## https://cygwin.com/cygwin-ug-net/using.html#pathnames-symlinks
# export CYGWIN="winsymlinks" # ln -s: The shortcut style symlinks with file extension '.lnk'
# export CYGWIN="winsymlinks:native" # ln -s: plain text file

## ln_hook
function ln(){
if [[ "-s" == "$1" ]]; then
cmd /C mklink /H "$(cygpath -aw "$3")" "`cygpath -aw "$2"`"
else
echo -e "\033[32m >>ln $* \033[0m"
command ln "$*"
fi
}

## Cannot propagate alias to other scripts
## alias ln='ln_hook'

## export -f ln=ln_hook ## ln_hook.sh: 第 23 行:export: ln=ln_hook: 不是函数

## http://docstore.mik.ua/orelly/unix3/upt/ch29_13.htm
## https://stackoverflow.com/questions/1885871/exporting-a-function-in-shell
export -f ln

echo&&echo "[^-^] After trying ln_hook"

echo -n "main shell: " && type ln

echo -n "subshell: " && (type ln)

echo&&echo "[^-^] Run an external script"

echo 'type ln' > test_ln.sh
./test_ln.sh
# $(dirname $0)/test_ln.sh

## . ./ln_hook
echo 'You can try: `type ln`, which will output: ln 是函数...'

Here, Demo or Bash shell editor and execute online

Why would I create an alias which creates a function?

I found this answer too [U&L] In Bash, when to alias, when to script, and when to write a function? which explains the benefit of defining a function in an alias.

The benefit of doing so over declaring a function is that your alias
cannot be simply overwritten by source-ing (or using .) a script which
happens to declare a same-named function.



Related Topics



Leave a reply



Submit