Multi line alias in bash

Jovin Miranda 09/02/2018. 2 answers, 974 views
command-line bash scripts alias

I have the following script. It's a simple test case where a is any string value and b is supposed to be a path.

#!/bin/bash

alias jo "\
echo "please enter values "\
read a \
read -e b \
echo "My values are $a and $b""

However whenever I try to execute ./sample.sh I get the following errors:

./sample.sh: line 3: alias: jo: not found
./sample.sh: line 3: alias: echo please: not found
./sample.sh: line 3: alias: enter: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: read a read -e b echo My: not found
./sample.sh: line 3: alias: values: not found
./sample.sh: line 3: alias: are: not found
./sample.sh: line 3: alias: and: not found
./sample.sh: line 3: alias: : not found

and when I try source sample.sh I get the following:

a: Undefined variable.

My aim was to make this an alias so that I can source this script and just run the alias to execute the line of commands. Can someone look at this and let me know what the error is?

2 Answers


steeldriver 09/02/2018.

You have a couple of issues here

  1. unlike in csh, in bash (and other Bourne-like shells), aliases are assigned with an = sign e.g. alias foo=bar

  2. quotes can't be nested like that; in this case, you can use single quotes around the alias and double quotes inside

  3. the backslash \ is a line continuation character: syntactically, it makes your command into a single line (the opposite of what you want)

So

#!/bin/bash

alias jo='
echo "please enter values "
read a 
read -e b 
echo "My values are $a and $b"'

Testing: first we source the file:

$ . ./myscript.sh

then

$ jo
please enter values 
foo bar
baz
My values are foo bar and baz

If you want to use the alias within a script, then remember that aliases are only enabled by default in interactive shells: to enable them inside a script you will need to add

shopt -s expand_aliases

Regardless of everything above, you should consider using a shell function rather than an alias for things like this


glenn jackman 09/02/2018.

Get used to using functions in the POSIX-type shell. You don't have any of the quoting issues:

jo () {
    read -p "Enter value for 'a': " -e a 
    read -p "Enter value for 'b': " -e b 
    echo "My values are $a and $b"
}

HighResolutionMusic.com - Download Hi-Res Songs

1 Alan Walker

Different World flac

Alan Walker. 2018. Writer: Shy Nodi;Alan Walker;Fredrik Borch Olsen;James Njie;Marcus Arnbekk;Gunnar Greve Pettersen;K-391;Corsak;Shy Martin;Magnus Bertelsen.
2 Skylar Grey

Everything I Need flac

Skylar Grey. 2018.
3

Tell Me It's Over

4 Ariana Grande

​Thank U, Next flac

Ariana Grande. 2018. Writer: Crazy Mike;Scootie;Victoria Monét;Tayla Parx;TBHits;Ariana Grande.
5 Julian Jordan

Glitch flac

Julian Jordan. 2018.
6 Mesto

Wait Another Day flac

Mesto. 2018.
7 The Chainsmokers

Hope flac

The Chainsmokers. 2018. Writer: Kate Morgan;Chris Lyon;Alex Pall;Andrew Taggart.
8 Anne-Marie

Rewrite The Stars flac

Anne-Marie. 2018. Writer: Benj Pasek;Justin Paul.
9 Ariana Grande

Imagine flac

Ariana Grande. 2018. Writer: Jameel Roberts;Priscilla Renea;Happy Perez;Andrew "Pop" Wansel;Ariana Grande.
10 Rita Ora

Let You Love Me flac

Rita Ora. 2018. Writer: Rita Ora;Easyfun;Fred Gibson;Noonie Bao;LotusIV;Ilsey Juber.
11 Alan Walker

Lily flac

Alan Walker. 2018.
12 Alan Walker

Lost Control flac

Alan Walker. 2018.
13 Emma Hewitt

Take Everything flac

Emma Hewitt. 2018. Writer: G. Emery;E. Hewitt;A. Hewitt.
14 Conor Maynard

How You Love Me flac

Conor Maynard. 2018. Writer: Yoshi Breen;Thom Bridges;Hardwell;Rik Annema;Conor Maynard;Cimo Fränkel;Snoop Dogg.
15 ZAYN

There You Are flac

ZAYN. 2018. Writer: Joe Garrett;Levi Lennox;Michael Hannides;Anthony Hannides;ZAYN.
16 Pollyanna

Starchild flac

Pollyanna. 2018.
17 (G)I-DLE

POP/STARS flac

(G)I-DLE. 2018. Writer: Riot Music Team;Harloe.
18 Fitz And The Tantrums

HandClap flac

Fitz And The Tantrums. 2017. Writer: Fitz And The Tantrums;Eric Frederic;Sam Hollander.
19 Mark Ronson

Nothing Breaks Like A Heart flac

Mark Ronson. 2018. Writer: Thomas Brenneck;Maxime Picard;Ilsey Juber;Conor Szymanski;Clement Picard;Mark Ronson;Miley Cyrus.
20 Alan Walker

I Don't Wanna Go flac

Alan Walker. 2018.

Related questions

Hot questions

Language

Popular Tags