Category Archives: linux

How do I use xargs with a function in bash?

Question: How do I use xargs with a function that I’ve defined in bash?

Answer:

The following example uses the export and runs in a new process which is why the export f switch is needed.


doEcho() {
  echo $1
}

export -f doEcho
echo You echo | xargs -d' ' -t -n1 -P2 bash -c 'doEcho "$@"' _

Note: This function works in bash and will not work in zsh.

In Ubuntu how can I use a shell alias that hasn’t taken effect yet

Question: In Ubuntu how can I use a shell alias that hasn’t taken effect yet?

Answer: After creating an alias during a session the alias doesn’t take effect often until you logout and log back in, an action which triggers a reparse of the alias file. You can immediately execute your new alias though with the eval command. Here is a brief example that will create a ‘test’ alias and map it to ‘ls’ to show you the current directory:


alias test=ls;eval test

Shortcut to clear the terminal window in Linux

Scenario:

You want a command line option to clear the terminal.

Solution:

Clearing the terminal window is highly dependant on what terminal software you’re using. You can create an alias that rights a clear code out that most terminals should support. The alias will allow you to map it to a command line “cls”.

Steps:

  1. Edit the “~/.bashrc” file
  2. Add the following line:
    alias cls="echo -ne '\033c'"
    
  3. Save the file. You may need to start a new terminal session for this to take effect.

Alternate Method for a Mac running OSX (Unix):

OSX supports a command called “reset”. Using this should clear the terminal window.