Welcome to the most active Linux Forum on the web.
Go Back > Blogs
User Name


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.

Make a bash script recognize bash aliases

Posted 05-12-2015 at 05:16 PM by the dsc (linux-related notes)
Updated 05-12-2015 at 05:22 PM by the dsc (mismatched tags)

Say, if you have a script that would take as a parameter something that you have aliased, it wouldn't work.

I mean:

$ ./ myalias whatever else
/home/dude/scripts/ line 3: myalias: command not found

In order to make it work, the said script must include:

shopt -s expand_aliases
source ~/.bash_aliases
(Or wherever your aliases are set, which would preferably be...
Posted in Uncategorized
Views 310 Comments 0 the dsc is offline

[bash] "keep trying" command, even if exits with error, for at least 10 times

Posted 08-08-2014 at 11:40 PM by the dsc (linux-related notes)
Updated 08-08-2014 at 11:42 PM by the dsc
Tags bash

A script that tries to run a given arbitrary command N times, or until it exits with success. The commented parts are aborted attempts to make it figure whether the first parameter is a number instead of a command, and that would be used as the maximum number of attempts. Something went wrong, sometime I'll try to figure it out.

# this program is stupid. It's declared to be in public domain by its author, who whishes to remain anonymous
Posted in Uncategorized
Views 336 Comments 3 the dsc is offline

Give a default/non-null value in order to avoid the "unary operator expected" error

Posted 02-19-2014 at 04:00 PM by the dsc (linux-related notes)
Tags bash

Originally Posted by cfajohnson
i am trying to compare a value with value 50. but i am getting
"[: -lt: unary operator expected"

I am using

if [ $j -lt 50 ]
echo "------------"

Make sure that $j expands to a value. Best is to set it explicitly, but you can also give it a default value (0 in this example):

if [ ${j:-0} -lt 50 ]
Posted in Uncategorized
Views 508 Comments 0 the dsc is offline

A good $PS1 for displaying long paths in the bash prompt

Posted 03-14-2013 at 12:29 AM by the dsc (linux-related notes)
Updated 03-14-2013 at 12:30 AM by the dsc
Tags bash, prompt, ps1

PS1='${debian_chroot:+($debian_chroot)}\t \u $(if [ ${#PWD} -gt 30 ] ; then myPWD="${PWD:0:12}/\W" ; myPWD="${myPWD:0:25}" ; else myPWD=$PWD; fi ; echo $myPWD) $ '
It will display full paths smaller than 30 characters entirely, but for longer paths it will get only the first characters of the whole path, and the fist characters of the last folder in the full path.

The end result is something like:

HH:MM:SS tuxguy /home/tuxguy/mystuf.../xmas...
Posted in Uncategorized
Views 879 Comments 0 the dsc is offline

Bash quickie: loop N times, without doing math

Posted 10-22-2012 at 10:07 PM by the dsc (linux-related notes)
Updated 03-10-2013 at 01:25 AM by the dsc (improving, varible version)
Tags bash, loop

I thought that the only way to do such type of loop in Bash would require something like "n=$(($n+1))" within the loop, with the loop being conditioned to "n" adding up to whatever you want, such as "until ((n==30)) ; do". But you can actually do "for i in {1..20} ; do <whatever> ; done". That's somewhat similar to Basic's "for i=1 to 20 ; whatever ; next i".

But it won't work with variables for the starting and ending numbers....
Posted in Uncategorized
Views 843 Comments 0 the dsc is offline


All times are GMT -5. The time now is 09:26 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration