Alias with variable in bash
I'd create a function for that, rather than alias, and then exported it, like this:
function tail_ls { ls -l "$1" | tail; }
export -f tail_ls
Note -f
switch to export
: it tells it that you are exporting a function. Put this in your .bashrc
and you are good to go.
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
Bash script add variable to alias
Forget the alias, and define a function.
ssh () {
command ssh -2 -o StrictHostKeyChecking=no -o FallBackToRsh=no -q -i /root/.ssh/identity "$@"
}
bash resolve aliases and variables
$ cat defs
alias x='ls -al'; alias y='df -kh' # etc
xx='name'; yy='stuff'
$ cat diffsets
#!/bin/bash
# Restart with a clean environment in case the file has been sourced
# previously. We need the absolute path in the shebang above for this.
[[ -v HOME ]] && exec -c "$0" "$@"
# We must use full paths without variables in case those variables
# are also set in the file we're about to source.
[[ -s "$1" ]] &&
mkdir -p "/usr/tmp/diffsets_$$" &&
trap '
rm -f "/usr/tmp/diffsets_$$/old" "/usr/tmp/diffsets_$$/new" &&
rmdir "/usr/tmp/diffsets_$$"
' 0 &&
# We want to only compare variables, not function definitions, but we
# can't use `set -o posix` as we need newlines printed as $'\n' instead
# of literal newline chars for later comparison so ensure posix is disabled
# and use awk to exit when the first function is seen as they always are
# printed after variables.
set +o posix &&
set | awk '$NF=="()"{exit} 1' > "/usr/tmp/diffsets_$$/old" &&
. "$1" &&
set +o posix &&
set | awk '$NF=="()"{exit} 1' > "/usr/tmp/diffsets_$$/new" &&
comm -13 "/usr/tmp/diffsets_$$/old" "/usr/tmp/diffsets_$$/new"
$ ./diffsets defs
xx=name
yy=stuff
I'm specifically not using variables to hold the set
output for comparison nor using mktemp
to create the temp files (which would require variables to hold the mktemp
output) as anything using variables in this script would fail if the file being sourced (defs
in this case) contained a definition for those variables.
Bash alias containing a variable of the current directory
Make sure to use single quotes when defining the alias to prevent bash from expanding ${PWD} at definition time:
alias ooo='./manage.py --settings="${PWD##*/}.settings.dev"'
.bash_profile alias with variables
From man bash
, under ALIASES
:
There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below)."
Thus:
create() { touch "$1"; code "$1"; }
How do I pass a variable to a bash alias easier nmap scanning? (Function, alias, script…?)
It's just a function.
qmap() {
nmap -sn "192.168.$1.1/24"
}
Reference variable in alias
Since you define your alias using double quotes, the $N
is expanded before definition and your alias actually is echo 4
.
Try this :
$N=4
$alias sayn='echo $N'
$N=2
$sayn
2
Related Topics
What Is the Linux Equivalent to Dos Pause
How to Set Rpath and Runpath with Gcc/Ld
How to Convert Yyyymmddhhmmss to a Date Readable by 'Date'
Sort Not Sorting as Expected (Space and Locale)
How to Remove All .Svn Directories from My Application Directories
How to Fix Conda Update Conda Permission Error
Insert Multiple Lines into a File After Specified Pattern Using Shell Script
How to Get Position of Cursor in Terminal
Why Does Printf Overwrite the Ecx Register
Nuget on Linux: Error Getting Response Stream
How Have Both Local and Remote Variable Inside an Ssh Command