How to Create an Alias in Linux

How to use alias command to create command with custom input?

Aliases do not accept parameters. You need to write a function or a separate script.

Bash/Zsh function:

Define the following function in your .bashrc/.zshrc depending on what interactive shell you use:

mycommand() {
oj d https://codeforces.com/contest/"$1"/problem/"$2"
}

Shell script:

Create the file mycommand with the following content in your $PATH and make it executable:

#!/bin/sh
oj d https://codeforces.com/contest/"$1"/problem/"$2"

$1 and $2 are positional parameters. If you call the script or function like $ mycommand 1348 A, $0 gets mapped to mycommand, $1 to 1348, $2 to A and so on. We put the double quotes around the variables in case they contain whitespace to prevent word splitting.

How can I create a command in linux, is alias the only way?

You have the command already: /opt/kafka/bin/kc.sh.

I understand that you want to make it available to your users as a simple kc.sh or kc, without the directory prefix. The solution is to either put that directory into the PATH variable, typically done in /etc/profile; or put the command respectively a (symbolic) link to it into a directory which is already in the PATH, like /bin/, /usr/bin or /usr/local/bin.

Putting the directory into the PATH is probably the better solution, also because there are probably more useful commands to be found. If you don't like kc.sh but want a simple kc you can still make a link in the same directory, e.g. by performing cd /opt/kafka/binand then ln -s kc.sh kc.

How to make an alias for a long path?

Since it's an environment variable (alias has a different definition in bash), you need to evaluate it with something like:

cd "${myFold}"

or:

cp "${myFold}/someFile" /somewhere/else

But I actually find it easier, if you just want the ease of switching into that directory, to create a real alias (in one of the bash startup files like .bashrc), so I can save keystrokes:

alias myfold='cd ~/Files/Scripts/Main'

Then you can just use (without the cd):

myfold

To get rid of the definition, you use unalias. The following transcript shows all of these in action:

pax> cd ; pwd ; ls -ald footy
/home/pax
drwxr-xr-x 2 pax pax 4096 Jul 28 11:00 footy

pax> footydir=/home/pax/footy ; cd "$footydir" ; pwd
/home/pax/footy

pax> cd ; pwd
/home/pax

pax> alias footy='cd /home/pax/footy' ; footy ; pwd
/home/pax/footy

pax> unalias footy ; footy
bash: footy: command not found

Create a bash alias for typing out part of a command

Well that's exactly the work alias is good for. You can just use

$ alias your-neat-shortcut="transmission-remote -a"

and then anytime you want use it you can write the in my case not so shorter version

$ your-neat-shortuct --some --aditional stuff and files

and bash will expand it for you to

$ transmission-remote -a --some --aditional stuff and files

Note that this thing will work only in your current session and if you want to have it there every time you log in, you want to put it in the file ~/.bashrc where ~ is bash way to represent your home directory. This file runs every time you start a new terminal (actually whenever you start new bash) so you'll have the alias ready then.

How do create an alias in shell scripts?

From the bash man page:

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the
description of shopt under SHELL BUILTIN COMMANDS below).

So this should work:

#!/bin/bash
shopt -s expand_aliases
alias I_am_only_ls_alias=ls
I_am_only_ls_alias

Scripts usually use functions, not aliases.

How to create string alias that can be combined with normal bash command?

You could use a small function such as this:

scpatoz()
{
if [ $# -eq 1 ]; then
scp -r abcdefghijklmnopqrstuvwxyz@helloworld.com:$1 .
elif [ $# -gt 1 ]; then
scp -r abcdefghijklmnopqrstuvwxyz@helloworld.com:$@
else
echo "Needs argument"
fi
}

then you could run scpatoz /home/a or add another argument to specify target location



Related Topics



Leave a reply



Submit