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
Create a bash alias with a variable in the name without space
No, in bash
, aliases work on individual words. So, unless you want to do:
alias 0x00='echo 00000000'
alias 0x01='echo 00000001'
then you're not going to get it working easily. It can be done (for a limited range) using that method by creating a script to source on the fly but it will result in a lot of aliases:
( for i in {0..255} ; do printf "alias 0x%02x='echo %d'\n" $i $i ; done ) >xyzzy_$$.sh
source xyzzy_$$.sh
rm xyzzy_$$.sh
Instead, I'd just opt for an function x
where you could do:
pax> x ff
11111111
There are no more keystrokes than you would need for 0xff
and you don't have to worry about trying to coerce bash
into doing something it's not really built to do.
Such a function can be created with:
pax> x() {
...> val=$(tr '[a-z]' '[A-Z]' <<< $1)
...> BC_LINE_LENGTH=0 bc <<< "ibase=16;obase=2;$val"
...> }
and used as follows:
pax> x ff
11111111
pax> x 42
1000010
pax> x cB
11001011
pax> x 457365384563453653276537456354635635326535635345
10001010111001101100101001110000100010101100011010001010011011001010011001001110110010100110111010001010110001101010100011000110101011000110101001100100110010100110101011000110101001101000101
Note the use of tr
to coerce alphas into uppercase values. Without that, you're likely to run into problems with bc
recognising them as valid hex digits.
Also note the setting of BC_LINE_LENGTH
to prevent bc
from auto-wrapping very large numbers, such as that last one where you would otherwise see:
pax> y 457365384563453653276537456354635635326535635345
10001010111001101100101001110000100010101100011010001010011011001010\
01100100111011001010011011101000101011000110101010001100011010101100\
0110101001100100110010100110101011000110101001101000101
Bash alias name from a variable
Did you even try it? Your first example works fine.
You can make the second work by adding an eval
:
create_func_with_name() {
eval "$1() {
echo inside a function with a variable name
}"
}
How to assign to a variable an alias
The way to provide an alias for a variable in C++ is to use references. For example,
int i = 42;
int& j = i; // j is an alias for i
const int& k = j; // k is an alias for i. You cannot modify i via k.
Create alias in shell script using a variable
I know it's a bash faq, but the ParsingLs guidelines apply here too. Don't parse the output of ls. Just don't. You have globs in csh too.
foreach i (`ls <path>/dir/*.tcl`)
should simply be
foreach i ( <path>/dir/*.tcl )
That said, the problem you're asking about is as Glenn suggested in comments. Single and double quotes behave in csh much the same way as they do in sh/bash: single quotes block variable expansion. Your alias is not actually setting an alias, it's setting something which when run will try to expand the variable at the time you run it. Try using double quotes and see if that gets you the behaviour you expect.
As an alternate strategy to shell aliases, consider linking or symlinking the script to multiple names on your path, then switching on $0
instead. It'll require less hacking on shells, which will be especially noticeable when someone decides to try out a different shell and forgets that these "commands" are really just shell aliases. :)
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.
How do I call a function in bash if there is an alias by the same name?
when I execute aaa the alias runs.
You can run it as:
\aaa
This will call function.
Related Topics
Git - Crlf Issue in Windows + Linux Dual Boot
How to Add More Algorithm in Cryptoapi in Linux
Can't Install Using Yum in Rhel 7.1
How to Make Linux Ignore a Keyboard While Keeping It Available for My Program to Read
How to Store Result of Diff in Linux
Snort Message - Warning: No Preprocessors Configured for Policy 0
How to Read N-Th Line from a Text File in Bash
Find' (Command) Finds Nothing with -Wholename
Remote Linux Server to Remote Linux Server Large Sparse Files Copy - How To
How to Delete 5 Lines Before and 6 Lines After Pattern Match Using Sed
Is There a Rpm File Naming Convention in Lsb
Getting Uptime or Downtime of Systemd Process Using Systemctl
Calculator in Assembly Language - Linux X86 & Nasm - Division