How to Define a Bash Alias as a Sequence of Multiple Commands

How can I define a bash alias as a sequence of multiple commands?

You mention BAT files so perhaps what you want is to write a shell script. If so then just enter the commands you want line-by-line into a file like so:

command1
command2

and ask bash to execute the file:

bash myscript.sh

If you want to be able to invoke the script directly without typing "bash" then add the following line as the first line of the file:

#! /bin/bash
command1
command2

Then mark the file as executable:

chmod 755 myscript.sh

Now you can run it just like any other executable:

./myscript.sh

Note that unix doesn't really care about file extensions. You can simply name the file "myscript" without the ".sh" extension if you like. It's that special first line that is important. For example, if you want to write your script in the Perl programming language instead of bash the first line would be:

#! /usr/bin/perl

That first line tells your shell what interpreter to invoke to execute your script.

Also, if you now copy your script into one of the directories listed in the $PATH environment variable then you can call it from anywhere by simply typing its file name:

myscript.sh

Even tab-completion works. Which is why I usually include a ~/bin directory in my $PATH so that I can easily install personal scripts. And best of all, once you have a bunch of personal scripts that you are used to having you can easily port them to any new unix machine by copying your personal ~/bin directory.

Multiple commands in an alias for bash

Try:

alias lock='gnome-screensaver; gnome-screensaver-command --lock'

or

lock() {
gnome-screensaver
gnome-screensaver-command --lock
}

in your .bashrc

The second solution allows you to use arguments.

Add multiple commands in single alias

It does execute the second command, but it does it in a subshell. When it exits the subshell, it goes back to the parent shell's directory.

What you want is a function, which executes in the current shell's environment. Try:

mkd() { mkdir -p $1 && cd $1; }

Aliases cannot take parameters, so they're not useful for what you want. You would call the above with something like mkd newdir. If you define this in your .bashrc, and it will work for all new sessions afterwards.

Bash: declaration of an alias to be used in a single lined multiple commands

According to the bash manual:

The rules concerning the definition and use of aliases are somewhat confusing.
Bash always reads at least one complete line of input before executing any of
the commands on that line. Aliases are expanded when a command is read, not
when it is executed. Therefore, an alias definition appearing on the same line
as another command does not take effect until the next line of input is read.

The commands following the alias definition on that line are not affected by the
new alias. This behavior is also an issue when functions are executed. Aliases
are expanded when a function definition is read, not when the function is executed,
because a function definition is itself a compound command. As a consequence,
aliases defined in a function are not available until after that function is executed.
To be safe, always put alias definitions on a separate line,
and do not use alias in compound commands.

For almost every purpose, aliases are superseded by shell functions.

Alias definition of multiple commands after ssh

For the ssh specifically, you're looking for the following:

ssh -t username@remotemachine "cd /path/you/want ; bash"

Using "&&" or even ";" normally will execute the commands in the shell that you're currently in. It's like if you're programming and make a function call and then have another line that you want to effect what happens in the function-- it doesn't work because it's essentially in a different scope.

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

Is there a concise way to define two or more bash aliases for the same expansion?


alias {c,e}="code ."

...will, after brace expansion, become:

alias c="code ." e="code ."

...which does what you want.


That said, I don't believe this question is on-topic here; aliases are an interactive facility not available by default in scripts, and Stack Overflow is exclusively scoped to software development. Writing scripts definitely counts; using your command-line shell, not so much.

How to call multiple git commands using one .gitconfig alias

If you want to combine add, commit and push, you'll need a bash function. Git add and commit can be combined with git -am "msg", but the push can only be done as an additional command. So, just define a bash function like this:

gacp() {
git add -A &&
git commit -m "${1?'Missing commit message'}" &&
git push
}

This works by doing the git add -A first, and if it succeeds, then the command git commit -m is executed, with the required message, and if that succeeds, then the git push is executed.

It's important to make the latter commands depend on successful execution of the previous commands in order to avoid downstream messes. In other words, you don't really want to commit changes unless the add succeeded, and you don't really want to push your most recent commits unless the commit succeeded.

You use it like this:

gacp "Latest changes"

Oh My Zsh multiple commands with one alias

As you've discovered, you can chain commands in a single alias using ;:

alias update_my_gems="echo foo; echo bar"

Alternatively, you can write a function very easily in your ~/.zshrc file:

update_my_gems() {
echo foo
echo bar
}

For readability, I'd personally go for a function for anything that's semi-complex.



Related Topics



Leave a reply



Submit