Tag Archives: bash

How to pass arguments to an alias in bash

Lets look at passing arguments to an alias by looking at an example.

The below command will open duckduckgo.com in a new tab in firefox.

firefox --new-window duckduckgo.com

Lets say, I want to create an alias but I want to pass the URL as a parameter to the alias.

For example, if I run the following command in the terminal, it should look at the first argument (which is the URL) and open it in a newtab in firefox.

ff duckduckgo.com

To achieve this, you can do the following:

Edit your ~/.bashrc or ~/.bash_profile using your favorite editor.

vim ~/.bashrc

Create a function inside the ~/.bashrc  with the following content. [Copy/paste the below inside your bashrc]

alias ff='function _ff()
{
firefox --new-window $1
};_ff'

Here, $1 is the first argument.

Once you save and close the bashrc file, you can source your bashrc for changes to take affect.

source ~/.bashrc

Now, if you enter the following command in the terminal, it will take “duckduckgo.com” which is the 1st parameter and open it in a new tab in firefox.

ff duckduckgo.com

Hope this helps. If you like my content, do share and subscribe for more content.

Source: StackOverFlow

Tmux not displaying bash prompt colors

Here is a screenshot of my terminal when I open tmux:

tmux PS1 prompt before

Note: Here, my shell displays my username@hostname but it does not display it in colors  🙁

To make tmux read your bash colors, edit your tmux configuration file:

vim ~/.tmux.conf

Add the following line to the tmux config file:

set -g default-terminal "screen-256color"

Exit and save the file. Close and re-open tmux.

Now, when you open tmux you should be able to get your regular bash prompt colors. Here is a screenshot after re-opening tmux:

 

tmux $PS1 bash prompt color after modying tmux.conf

Hope this helps! Cheers!

 

Source: Link

Useful Linux Terminal shortcuts

Here are some useful Linux Terminal Shortcuts:

Shortcut 1: Run previous typed command

In your Linux terminal, type:

# ls -l

Now, if you want to run this command again i.e the (previous command), type the following and hit ENTER:

# !!

This will run the command which was previously typed i.e ‘ls -l’.

Shortcut 2: Run command with last argument from previous command

For this example, lets create an empty file one.txt:

# touch one.txt

Now, lets use the following command:

# cp one.txt one.bak

Lets break down this command according to its arguments:

1st argument – one.txt

2nd argument / last argument – one.bak

[ Note: We can logically say that the 0th argument is the command cp itself. ]

Lets say we want to edit the file “one.bak” (which is the last argument for command cp, we can use:

# vi !$

The above command is same as running ‘vi one.back‘. Also note that the !$ passes the last argument of the previous command to the current command vi.

Shortcut 2 (Alternative):

Now, lets use the following command:

# cp one.txt one.bak

An alternative way to provide an argument from the previous command to another command is by using !:<argument number>. For example, to pass the 2nd argument of the last command, we use !:2 as follows:

# vi !:2

Here !:2 means that we are passing the 2nd argument of the previous command.

Shortcut 3 : Linux Terminal Keyboard shortcuts

To get the previous command: Press the UP arrow in the keyboard.

To kill a process when you are in the Linux terminal: Press <CTRL> + C in your keyboard.

Shortcut 4 : Autocomplete:

Trust me when I say this, this shortcut makes your life easier. If you need to auto complete a command, you can use the TAB key in your keyboard. This is one the shortcuts that is used by most Linux users.

Example:

If you want to type ifconfig in your Linux terminal, you can start typing the command and then press the TAB key to auto complete it. In this case, type ifcf and then press TAB and it will autocomplete the command.

The TAB feature can also be used to autocomplete known information such as file names which are passed as arguments.

For example: Lets assume in the current directory that you are on has only two files first.txt and second.txt. Now, if you need to type this command ‘cat first.txt second.txt‘, then you can use the TAB shortcut to auto-complete it by following these steps:

Type cat and then type f and then press TAB to autocomplete it to:

 cat first.txt

Then type the first letter of the next argument i.e. type s and then press TAB, which it autocomplete the command to:

cat first.txt second.txt

Shortcut 5: Move between words

  • Navigate to the next word from current position:

Press <CTRL> and <Right Arrow key> in your keyword to move to the next word(towards the right side of current cursor position) in your terminal. This is also applicable in Linux editors such as vi or vim.

  • Move to previous word in terminal from current position:

Press <CTRL> and <Left Arrow key> in your keyword to move to the previous word/the word to the left side from current cursor position in your Linux terminal (or most editors). You can press it again accordingly, if you need to move the cursor one word at a time towards the left direction.

The above two shortcuts are useful when you are entering commands in the Linux terminal and need to correct a particular word in the command. This helps navigating back and forth between words/parameters in the command with ease.

Other Bash Shortcuts:

To go to the 1st terminal, press CTRL, ALT and the function key F1 in your keyboard.

To go the nth terminal, press CTRL, ALT and Fn (where Fn can be function keys F1, F2, F3 etc).

To paste previously copied text to your Linux terminal/editor, you can press the middle button in your mouse. Now, How cool is that uh? 🙂

To lock your Linux machine press <CTRL> <ALT> and the letter l in your keyboard.

And that’s it for now, hope its been informative. I will be updating this article in the future with more Linux terminal shortcuts  as soon as I find new ones, so stay tuned! If this article helped you, do leave a comment below and like us on Twitter and Facebook. Cheers!

Hope this helps!