Combining bash aliases and arguments/parameters [duplicate] - bash

This question already has answers here:
Make a Bash alias that takes a parameter?
(24 answers)
Closed 1 year ago.
Let's say I have a bash script called script.sh and I want to create an alias runscript. Now, I know how to create an alias in my bash_profile or bashrc.
However - if I want to run parameters and do the following
$ runscript param1 param2
Is there something special I need to write in the script or in the alias that allows me to run the alias and the use parameters as well?

Based on your other question - "Accessing Shell parameters inside functions" - here is an example:
$ ls script
script
$ cat script
#!/usr/bin/env bash
_aFunction() {
echo "Parameter 1: ${1}"
echo "Parameter 2: ${2}"
}
_aFunction
_aFunction "$1" "$2"
_aFunction One Two
$ alias my_alias="./script"
$ my_alias 1 2
Parameter 1:
Parameter 2:
Parameter 1: 1
Parameter 2: 2
Parameter 1: One
Parameter 2: Two

Related

Printing arrays are not working in shell script [duplicate]

This question already has answers here:
Why does my Bash code fail when I run it with 'sh'?
(2 answers)
Closed 10 months ago.
I am kind of new in shell scripting and trying to learn arrays. I declared array value but when I am trying to print that array it is giving me an error(bad substitution).
I am pasting the code below, please suggest to me what is wrong here-
➜ ~ cat test.sh
#!/bin/bash
array=['foo','bar','a','b']
echo 1
echo "${array[0]}"
➜ ~ sh test.sh
1
test.sh: 5: Bad substitution
Thanks in advance.
Depending on the system you're using sh might be not be Bash and
it's not Bash on yours, it can be dash for example. Run your script with Bash:
$ bash arr.sh
1
[foo,bar,a,b]
Or set an executable bit and call the script without providing the name of the interpreter since you already have the shebang:
$ chmod +x test.sh
$ ./test.sh
1
[foo,bar,a,b]

How can I create a parameterized bash alias? [duplicate]

This question already has answers here:
Make a Bash alias that takes a parameter?
(24 answers)
Closed 7 years ago.
I would like to create a parameterized bash alias. I am not certain that is the correct way to describe it.
For example:
alias gps="gulp protractor --specs=test/browser/specs/<i-want-to-parameterize-this-bit>.js"
So that I can type:
gps foo/bar
...and the command resovles to:
alias gps="gulp protractor --specs=test/browser/specs/foo/bar.js"
How can I do this?
Use a shell function:
gdiff() {
git diff --color=always "$#" | less -r
}
Another example:
foo() { /path/to/command "$#" ;}
foo arg1 arg2
You can create script, and pass arg.

How to send BASH variables to multiple scripts? [duplicate]

This question already has answers here:
Pass all variables from one shell script to another?
(7 answers)
Closed 8 years ago.
I have many BASH scripts called in sequence, e.g., script1.sh contains:
#!/bin/bash
bash script2.sh
bash script3.sh
bash script4.sh
script2.sh contains:
#!/bin/bash
file_a="1.txt"
cp $file_a /tmp/$file_a.tmp
script3.sh contains:
#!/bin/bash
wc -l /tmp/$file_a.tmp
script4.sh contains:
#!/bin/bash
cat /tmp/2.txt $file_a.tmp > file3.txt
Each file requires access to a small collection of variables. How can I pass the variables from one script onto the next?
You have many options.
The first method would be making the variable as the environment variable and pass to the script before the second script get executed.
The second method would be making the second script to run in the same shell.
The methods are described well here with the examples.

Bash re-assignment of value to variable "command not found" [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 8 years ago.
Any ideas why this is happening? Why do I have to manually explicitly reassign the variable but can't do it if I have another variable in the name of the variable?
SCRIPT:
#!/bin/bash
a_1=1
a_2=1
for temp in 1 2
do
a_$temp="2"
echo $((a_$temp))
done
a_1=2
a_2=2
echo $a_1
echo $a_2
OUTPUT:
[dgupta#della4 Rates_Of_Quenching]$ ./test.sh
./test.sh: line 8: a_1=2: command not found
1
./test.sh: line 8: a_2=2: command not found
1
2
2
Instead of:
a_$temp="2"
Use:
declare a_$temp="2"
to create variable with dynamic name.
As far as bash is concerned, you are trying to execute the command 'a_1=2', rather than perform an assignment. You can get around this by using declare, or its synonym typeset:
'a_1=2' # bash: a_1=2: command not found
typeset 'a_1=2'
echo $a_1 # 2
declare 'a_1=3'
echo $a_1 # 3
While it is possible to use declare, you might want to take advantage of bash arrays (which have been around since bash version 2) rather than using variables with numerical suffixes:
a=(1 1)
echo ${a[0]} # 1
echo ${a[1]} # 1
for i in 0 1; do a[i]=2; done
echo ${a[0]} # 2
echo ${a[1]} # 2

How to pass command line arguments to a shell alias? [duplicate]

This question already has answers here:
Make a Bash alias that takes a parameter?
(24 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
How do I pass the command line arguments to an alias? Here is a sample:
alias mkcd='mkdir $1; cd $1;'
But in this case the $xx is getting translated at the alias creating time and not at runtime. I have, however, created a workaround using a shell function (after googling a little) like below:
function mkcd(){
mkdir $1
cd $1
}
Just wanted to know if there is a way to make aliases that accept CL parameters.
BTW - I use 'bash' as my default shell.
Just to reiterate what has been posted for other shells, in Bash the following works:
alias blah='function _blah(){ echo "First: $1"; echo "Second: $2"; };_blah'
Running the following:
blah one two
Gives the output below:
First: one
Second: two
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.
You cannot in ksh, but you can in csh.
alias mkcd 'mkdir \!^; cd \!^1'
In ksh, function is the way to go. But if you really really wanted to use alias:
alias mkcd='_(){ mkdir $1; cd $1; }; _'
To quote the bash man page:
There is no mechanism for using arguments in the replacement text. If
arguments are needed, a shell function should be used (see FUNCTIONS
below).
So it looks like you've answered your own question -- use a function instead of an alias
You may also find this command useful:
mkdir dirname && cd $_
where dirname is the name of the directory you want to create
The easiest way, is to use function not alias. you can still call a function at any time from the cli. In bash, you can just add function name() { command } it loads the same as an alias.
function mkcd() { mkdir $1; cd $1 ;}
Not sure about other shells
I found that functions cannot be written in ~/.cshrc file. Here in alias which takes arguments
for example, arguments passed to 'find' command
alias fl "find . -name '\!:1'"
Ex: >fl abc
where abc is the argument passed as !:1
You actually can't do what you want with Bash aliases, since aliases are static. Instead, use the function you have created.
Look here for more information: http://www.mactips.org/archives/2008/01/01/increase-productivity-with-bash-aliases-and-functions/. (Yes I know it's mactips.org, but it's about Bash, so don't worry.)
This works in ksh:
$ alias -x mkcd="mkdir \$dirname; cd \$dirname;"
$ alias mkcd
mkcd='mkdir $dirname; cd $dirname;'
$ dirname=aaa
$ pwd
/tmp
$ mkcd
$ pwd
/tmp/aaa
The "-x" option make the alias "exported" - alias is visible in subshells.
And be aware of fact that aliases defined in a script are not visible in that script (because aliases are expanded when a script is loaded, not when a line is interpreted). This can be solved with executing another script file in same shell (using dot).
Here's a simple example function using Python. You can stick in ~/.bashrc.
You need to have a space after the first left curly bracket.
The python command needs to be in double quotes to get the variable substitution. Don't forget that semicolon at the end.
count(){ python -c "for num in xrange($1):print num";}
Example run:
$ count 6
0
1
2
3
4
5
$
An empty alias will execute its args:
alias DEBUG=

Resources