bash – echo command with option to display the output on the same line.

2010-09-21 2 min read Bash Fedora Learning Linux

Here is a nice little trick to display the output in the same line :

  <td>
    <div class="bash codecolorer">
      &nbsp;<span class="kw3">echo</span> <span class="re5">-ne</span> <span class="st0">"output\<span class="es1">\r</span>"</span>
    </div>
  </td>
</tr>
1

The trick here is simple, first we disable the <a class="zem_slink" title="Newline" rel="wikipedia" href="http://en.wikipedia.org/wiki/Newline">newline printed by <a class="zem_slink" title="Echo (command)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Echo_%28command%29">echo command with the &#8221;-n&#8221; option and then we enable the interpretation of the <a class="zem_slink" title="Backslash" rel="wikipedia" href="http://en.wikipedia.org/wiki/Backslash">backslash characters with the &#8221;-e&#8221; option. As the &#8221;\r&#8221; is used as <a class="zem_slink" title="Carriage return" rel="wikipedia" href="http://en.wikipedia.org/wiki/Carriage_return">carriage return which brings you to the start of the line without the newline (like in <a class="zem_slink" title="Typewriter" rel="wikipedia" href="http://en.wikipedia.org/wiki/Typewriter">typewriter), we will use the same to get the desired effect. So, if you wanted a stopwatch for a 100 seconds on the console then this simple bash <a class="zem_slink" title="For loop" rel="wikipedia" href="http://en.wikipedia.org/wiki/For_loop">for loop would do the same:

Continue reading

Remove some path from the PATH variable temporarily.

2010-09-04 2 min read Bash Linux

How many times has it happened to you that you are working on some linux platform (like Fedora/Ubuntu/CentOS etc) and suddenly you see that you need to remove some path from the PATH variable so that a script is executed from some other path. It really difficult to do this if the path is too long and if you end up doing this couple of times. If that is the case, then the below script is for you 🙂

Continue reading

Use the last command\’s output as input to a command without piping and bind to it to a key sequence in bash.

2010-09-01 1 min read Fedora Linux Uncategorized

Use the last command&#8217;s output as input to a command without piping and
bind to it to a key sequence in <a class="zem_slink" title="Bash (Unix shell)" rel="homepage" href="http://tiswww.case.edu/php/chet/bash/bashtop.html">bash.

  <td>
    <div class="bash codecolorer">
      &nbsp;<span class="kw3">bind</span> <span class="co3">\'</span><span class="st0">"\\C-h"</span>: <span class="st0">"\\<span class="es5">`fc\\ \\-s\\`</span>"</span><span class="co3">\'</span>
    </div>
  </td>
</tr>
1

My comment : fc is the fix command of the bash builtin.
This is similar to using `!!` or

Continue reading

tee to a file descriptor

2010-08-13 1 min read Bash Linux

tee to a file descriptor

1
$ tee >(cat &#8211; >&2)

the tee command does fine with file names, but not so much with file descriptors, such as &2. This uses process redirection to tee to the specified descriptor.

In the sample output, it’s being used to tee to stderr, which is connected with the terminal, and to wc -l, which is also outputting to the terminal. The result is the output of bash –version followed by the linecount

Continue reading

Magic space on the current command line

2010-06-25 1 min read Bash Fedora Linux

# For how to set up magic space for Bash see:
# – http://www.ukuug.org/events/linux2003/papers/bash_tips/#slide15
# – http://codesnippets.joyent.com/posts/show/1690

man bash 2>/dev/null | less -p &#8217;magic-space&#8217;

man bash 2>/dev/null | less -p &#8217;Event Designators&#8217;

man bash 2>/dev/null | less -p &#8217;\!#&#8217; # &#8221;The entire <a class="zem_slink" title="Command-line interface" rel="wikipedia" href="http://en.wikipedia.org/wiki/Command-line_interface">command line typed so far.&#8221;

<a href="http://snippets.dzone.com/posts/show/10265">Read more :<h6 class="zemanta-related-title" style="font-size: 1em;">Related articles by Zemanta <ul class="zemanta-article-ul"> <li class="zemanta-article-ul-li"><a href="http://www.makeuseof.com/tag/trigger-wake-lan-mac-address/">How To Trigger Wake On LAN Using The MAC Address (makeuseof.com) <li class="zemanta-article-ul-li"><a href="http://smarterware.org/4271/projectview-lists-your-todo-txt-by-project">Projectview Lists Your Todo.txt by Project (smarterware.org) <li class="zemanta-article-ul-li"><a href="http://helpdeskgeek.com/linux-tips/shell-script-backup-directory-linux/">Back Up a Directory in Linux using a Shell Script (helpdeskgeek.com) <div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/038d79c3-59e6-41ad-a8ad-31d1af669509/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/reblog_e39.png" alt="Reblog this post [with Zemanta]" /><span class="zem-script more-related pretty-attribution">

Continue reading

cdargs – bash cd command with bookmarks and browser.

2010-06-22 0 min read Bash Learning Linux
\"Screenshot
Image via Wikipedia

cdargs is a <a class="zem_slink freebase/en/bash" title="Bash" rel="homepage" href="http://tiswww.case.edu/php/chet/bash/bashtop.html">bash shell cd with bookmarks and browser. Quite useful if you are developer or someone who use&#8217;s the bash shell very frequently

Name       : cdargs

Arch : i686 Version : 1.35 Release : 5.fc12 Size : 114 k Repo : installed From repo : fedora Summary : The shell cd with bookmarks and browser URL : http://www.skamphausen.de/software/cdargs/ License : <a class="zem_slink freebase/en/gnu_general_public_license" title="GNU General Public License" rel="wikipedia" href="http://en.wikipedia.org/wiki/GNU_General_Public_License">GPLv2</a>+ Description: Enables the user to quickly change <a class="zem_slink freebase/en/working_directory" title="Working directory" rel="wikipedia" href="http://en.wikipedia.org/wiki/Working_directory">working directory</a> by navigating cd arguments : and expanding the shell built-in cd with bookmarks and browser.

Continue reading

colors in bash -- script to display all the possible colors.

2010-06-06 3 min read Bash Fedora Linux

If you wanted to have colors in the bash output (including the colors in PS1-4), don&#8217;t you keep wondering how the color code would look on the terminal. So, I wrote this small script to show the complete color codes. This is how the output would look:

\"Bash

And here is the script:

  <td>
    <div class="text codecolorer">
      #!/bin/bash -<br /> #===============================================================================<br /> #<br /> # &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE: &nbsp;colors.sh<br /> #<br /> # &nbsp; &nbsp; &nbsp; &nbsp; USAGE: &nbsp;./colors.sh<br /> #<br /> # &nbsp; DESCRIPTION: &nbsp;Bash colors<br /> #<br /> # &nbsp; &nbsp; &nbsp; OPTIONS: &nbsp;---<br /> # &nbsp;REQUIREMENTS: &nbsp;---<br /> # &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BUGS: &nbsp;---<br /> # &nbsp; &nbsp; &nbsp; &nbsp; NOTES: &nbsp;---<br /> # &nbsp; &nbsp; &nbsp; &nbsp;AUTHOR: &nbsp;Amit Agarwal (AKA), amit.agarwal@amit-agarwal.co.in<br /> # &nbsp; &nbsp; &nbsp; COMPANY: &nbsp;Individual<br /> # &nbsp; &nbsp; &nbsp; VERSION: &nbsp;1.0<br /> # &nbsp; &nbsp; &nbsp; CREATED: &nbsp;09/21/2009 06:12:07 PM IST<br /> # &nbsp; &nbsp; &nbsp;REVISION: &nbsp;---<br /> #===============================================================================<br /> <br /> for c in `seq 0 255`;<br /> do<br /> t=5;<br /> [[ $c -lt 108 ]]&&t=0;<br /> for i in `seq $t 5`;<br /> do<br /> #Display the codes also for easier lookup in terminal<br /> <!--loginview start-->echo $i;${c}<!--loginview end--><br /> echo -e "<a class="zem_slink" title="Path (computing)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Path_%28computing%29">\\</a>\e[0;48;$i;${c}m|| $i:$c `seq -s+0 $(($COLUMNS/2))|tr -d \'[0-9]\'`\\e[0m";<br /> done;<br /> done<br /> <br /> # setup_colors - Adds colors to array CC for global use<br /> # 30 - Black, 31 - Red, 32 - Green, 33 - Yellow, 34 - Blue,<br /> # 35 - Magenta, 36 - Blue/Green, 37 - White,<br /> # 30/42 - Black on Green \'30\\;42\'<br /> <!--loginview start-->function setup_colors(){<br /> declare -a CC;<br /> for i in `seq 0 7`;<br /> do<br /> ii=$(($i+7));<br /> CC[$i]="\\033[1;3${i}m";<br /> CC[$ii]="\\033[0;3${i}m";<br /> done;<br /> CC[15]="\\033[30;42m";<br /> R=$\'\\033[0;00m\';<br /> X=$\'\\033[1;37m\';<br /> export R X;<br /> }<br /> function display_colors(){<br /> for i in $(seq 0 $((${#CC[@]} - 1))); do echo -e "${CC[$i]}[$i]\\n$R"; done<br /> }
    </div>
  </td>
</tr>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
\"Reblog
Enhanced by Zemanta
Older posts Newer posts