General discussion


How do I make bash scripting interesting?

By Jio ·
I'm a postgraduate student and I chose a project in Linux because I liked the challenge of learning something new. This is my first experience with Linux OS and bash scritping.

After 2 months, I've still not fallen in love with linux and bash scripting. I'm not finding them as interesting as I found Windows, Java and VB when I was learning them.

Please, what do I need to do to make Linux and especially bash scripting appealing to me so that I can reduce my learning curve?

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

When you find out let me know

by stress junkie In reply to How do I make bash script ...

The people who made the earliest contributions to Unix were probably gifted computer scientists but they didn't have a clue about user interfaces. All of the shell scripts are difficult to learn and, as you say, just not very enjoyable to use. The documentation was extremely poor back-in-the-day which added to the pain of learning and using these tools. The documentation has gotten a lot better in the last 10 years largely due to the Linux project.

I was going to provide a couple of links to online scripting information but I realized while I was writing this that you didn't ask for user guides et. al. You asked to find a way to make scripting more enjoyable. I wish I knew how to do that. I get very frustrated with stupid syntax, metacharacters, regular expressions, and redirecting input/output. I think that one good solution would be to invent a good, easy to use scripting language/shell. As far as I'm concerned the bash, sh, csh, and tcsh shells are a real pain to use.

I'll be watching this discussion to see if anyone has any GOOD ideas.

Collapse -

I need a very good HOWTO

by Jio In reply to When you find out let me ...

Thanks and at least now I know I'm not the only one who's found shell scripting a pain.

I would appreciate links to tutorials because I'm finding the O'Reilly book that I bought on learning bash too complex.

I believe a HOWTO tutorial for newbies will go a long way to help me understand bash.


Collapse -

Is learning interesting?

by masinick In reply to How do I make bash script ...

What makes learning interesting for you in general?
Do you like lots of examples?
Do you like some compelling reason to use a particular tool?

The appeal of scripting languages in general are that they are interpretive. You can test out the results of what you do immediately.

The appeal of BASH is that it is a scripting language, it has a lot of conveniences that you can use to make using your system easier.

I think that the references provided are good ones. Perhaps a few examples will also help you out.

Here is a slightly modified version of my login bash script, .bashrc. I changed a few names, the rest is intact:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If running interactively, then:
if [ "$PS1" ]; then

# enable color support of ls and also add handy aliases

eval `dircolors -b`
alias c='clear'
alias d='cd /downloads/download'
alias e='nohup /usr/bin/emacs > /dev/null 2>&1 &'
alias g='cd $HOME'
alias h='history'
alias gf='nohup gftp > /dev/null 2>&1 &'
alias home='cd $HOME'
alias l='ls -lA'
alias la='ls -A'
alias lf='ls -F'
alias ll='ls -l'
alias ls='ls --color=auto'
alias m='more'
alias nh='nohup nedit > /dev/null 2>&1 &'
alias null="~/null.bash"
alias windir="cd /windowsxp/Documents\ and\ Settings/John\ Doe/My\ Documents"
alias up='cd ..'

# set a fancy prompt

PS1='\u@\h\n\w \t [\!]\$ '

# If this is an xterm set the title to user@host:dir
#case $TERM in
# PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
# ;;
# ;;

if [ "$TERM" = "xterm" ]; then
PS1="\033]2;\u@\h:\w\007\u@\h\n\w \t [\!]\$ "


if [ `whoami` = "root" ]; then
PS1='\u@\h\n\w \t [\!]# '
export PATH

# set PATH so it includes user's private bin if it exists

if [ -d ~/bin ] ; then
export PATH

function addpath
echo $PATH

# set MAIL environment so that mail can be extracted from Mozilla
# Mail

export MAIL
export BROWSER

Collapse -


by masinick In reply to Is learning interesting?

I wanted to be able to quickly and easily start up programs in the background from a terminal console. Sure, I could use a File Browser or a menu system, but frankly, those things often slow me down. Though I am hardly a great typist, when I type things in often enough, it is sometimes easier to type in a half dozen to a dozen letters and press Enter than it is to navigate menus.

So I wrote a very short script that allows me to start up applications, but then immediately regain control of my terminal console. The script takes either one or two arguments. Like anything else, it could be generalized much more than that, but it suits my needs.

Here is null.bash:

if [ -z "$1" ] # Checks if parameter #1 is zero length
echo "No program parameter provided"
return 1
if [ -z "$2" ] # Checks if parameter #2 is zero length
nohup $1 > /dev/null 2>&1 &
nohup $1 $2 > /dev/null 2>&1 &

Collapse -

Getting fancy with BASH prompts

by masinick In reply to null.bash
is a handy dandy tool to help you get "cute" with the command line prompt when you use BASH as your interactive shell.

Collapse -

~/.bashrc for aliases

by joe In reply to Is learning interesting?

While it's not manditory, alias defs should be put in ~/.bashrc and sourced from there. Like this, at the beginning of your script -

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc

Collapse -

Useful documentation

by masinick In reply to How do I make bash script ...

Mandrake Linux, recently renamed Mandriva, has always catered to creating reasonably easy to use software that is still powerful and flexible. Because of their orientation, they still provide direct access to the flexible stuff that makes Linux software so powerful, but they also provide a lot of tools to make the learning a bit easier. Over the years, their documentation has improved consideraably.
is a nice document that covers the command line and how it works. You can navigate elsewhere through that site to find a lot of other useful documentation, but perhaps this is something else to make your life a bit easier.

Collapse -


by crvmp3 In reply to How do I make bash script ...

Well, I would think that until you are really into using linux that'll you won't find many interesting things to script. However, in the mean time why don't you try to write simple text based games like tic-tac-toe or checkers. I find games are the best way to exercise a scripting or programming language. Other things to try scripting is creating an automated installer for some application that doesn't have one -- you could probably find something on; also you could trying scripting a database backup procedure to back up MySQL or Postgres database, ftp to another location (perhaps on the same machine) and do a restore...

Anyway, good luck!

Collapse -


by joe In reply to How do I make bash script ...

One thing that might help is to understand that bash scripting is not like batch scripting. With bash you can do a lot of the things that you might initially think of using C or VB or even Java for. bash, or any modern shell for UNIX, is a full featured programming language/environment. I have see whole applications that were done using only bash, Korn or even Bourne shells. The suggestion to try writing a game is not a bad one but if you can't think of a game you can definitely use google to get more scripting examples that you can ever want. The pointers to the docs are worth reading as well.

Fun is relative, though. You might not find bash fun because you're more interested in visual/GUI things (MS Windows, Java & VB are all GUI orientated). There's nothing wrong with that. If you find that this is the case then one thing you can do is to learn bash as you go. Just learn enough to figure out how to do a task you need and worry about learning more when you have more complex tasks to accomplish. Just a thought.

Related Discussions

Related Forums