Alias has no effect in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alias has no effect in script
# 1  
Old 12-22-2012
Alias has no effect in script

It doesn't have effect in script but it works on the terminal
Code:
root@server:/opt/kvm/usecases/logs# alias echo='echo -e'
root@server:/opt/kvm/usecases/logs# echo "xxxx\n"
xxxx
root@server:/opt/kvm/usecases/logs# cat xx.sh
#!/bin/bash
alias echo='echo -n'
echo  "sssf \n"

root@server:/opt/kvm/usecases/logs# ./xx.sh 
sssf \n

Code:
alias ssh='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
also has no effect in script

# 2  
Old 12-22-2012
echo is a shell builtin and a UNIX command in /usr/bin. The command version in /usr/bin accepts -e

So:

Code:
alias echo='/usr/bin/echo -e'

You are better off off specifying which echo:
Code:
/usr/bin/echo -e "sssf \n"

is you best choice
# 3  
Old 12-22-2012
Quote:
Originally Posted by jim mcnamara
echo is a shell builtin and a UNIX command in /usr/bin. The command version in /usr/bin accepts -e

So:

Code:
alias echo='/usr/bin/echo -e'

You are better off off specifying which echo:
Code:
/usr/bin/echo -e "sssf \n"

is you best choice
also has no effect

Code:
root@server:/opt/kvm/usecases/logs# ./xx.sh
sssf \n
root@server:/opt/kvm/usecases/logs# cat xx.sh
#!/bin/bash
alias echo='/bin/echo -e'
echo  "sssf \n"
root@server:/opt/kvm/usecases/logs# which echo 
/bin/echo

# 4  
Old 12-22-2012
You can fix this by adding shopt -s expand_aliases
Code:
#!/bin/bash
shopt -s expand_aliases
alias echo='echo -e'
echo  "sssf \n"

Note: aliases are not expanded by default in non-interactive shell, and it can be enabled by setting the expand_aliases shell option using shopt.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Create alias files (not alias commands)

If one: $ find -name 'some expression' -type f > newfile and then subsequently wants to create an alias file from each pathname the find command retrieved and the > placed within 'newfile', how would one do this? Ideally, the newly created alias files would all be in one directory. I am... (3 Replies)
Discussion started by: Alexander4444
3 Replies

2. Shell Programming and Scripting

Alias not recognized within script

What would cause a script to work under one user account and not another? Here's an example of what I'm referring to. Here's a simple script. Let's put it in a file called “thescript”. #! /bin/bash alias a='echo hello world' a Here are the results when this script is executed logged in... (3 Replies)
Discussion started by: sszd
3 Replies

3. Shell Programming and Scripting

Using alias in script

Hi all, Could any one tell me how to use aliases in a script i have a large command which launches an application. I want to use alias in the script because i call the application many time in the shell script. Thanks Firestar. (5 Replies)
Discussion started by: firestar
5 Replies

4. UNIX for Advanced & Expert Users

A formatted df script or alias ... ???

Hi gurus, Does anyone have a df script/alias that is sort of "universal"? Just getting frustrated to use bdf for HP-UX, df -h for Linux and Solaris, df -G/g (???) for AIX ... and to make things even worse, some are NFS mount points or with long logical volume name and it extends over two (2)... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. UNIX for Dummies Questions & Answers

kill script using alias name

Hi, I am using the below command to kill the firefox process i have opened in Redhat 5. ps -ef|grep fire|grep -v grep|awk '{print $2}'|xargs kill -9 If i execute the above command in terminal it works good and kills session. but when i use alias for that it is not working. alias... (2 Replies)
Discussion started by: nokiak810
2 Replies

6. Shell Programming and Scripting

Nullify the effect of Trap command in later part of the script

Hi All, i have an issue regarding trap command. i have specified trap function in the beginning of the script to catch some signals but in the later part of the script i want to remove the effect of this. Can anybody help me out of this. for e.g. pressing Ctrl+C for the first time should... (2 Replies)
Discussion started by: vikas_kesarwani
2 Replies

7. UNIX for Advanced & Expert Users

alias command in script

How can I embed alias command inside the unix script? Script: echo "...." ... ... alias aa=/usr/bin/telnet ... ...The above script is not working. If I type aa hostname in the command prompt 'TELNET' terminal is not opening. Regards,... (2 Replies)
Discussion started by: sharif
2 Replies

8. Solaris

will this script in crontab effect SUN 9??

Hi all, I work in Sun Solaris 9. I am plan to put the following script(remove90dysOldrfiles.sh) in CRONTAB for removing huge huge number of files those are older than 90 days from different directory. In the Crontab i will set the time for everymidnight it will search 90days older file and... (2 Replies)
Discussion started by: thepurple
2 Replies

9. UNIX for Dummies Questions & Answers

How do properties effect script?

Hi, I have noticed that rm -if will perform completely different to rm -fi. Whats the pattern of how I put my options to the script in relation to how it will act. i.e rm -fi treat the remove as interative but rm -if treats it as forced Thansk, Chris. (1 Reply)
Discussion started by: Chiefos
1 Replies

10. Shell Programming and Scripting

Auto Alias Script

Hi All, as you can guess, I'm a newb here, and to shell scripting. I use Linux(CentOS) at home and want to get into scritping. I was creating some aliases on a Laptop the other day, and thought, god this is tedious, and then I thought, heck, why not use it as a 1st script project. Please see... (3 Replies)
Discussion started by: coolboarderguy
3 Replies
Login or Register to Ask a Question