Alias not recognized within script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alias not recognized within script
# 1  
Old 01-27-2012
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”.

Code:
#! /bin/bash
alias a='echo hello world'
a

Here are the results when this script is executed logged in under one user account.

Code:
$ thescript
hello world

Here are the results when this script is executed logged in from a different user account.

Code:
$ thescript
command 'a' not found

The second account doesn't seem to recognize the defined alias within the script. And I say within the script because I can manually define an alias from the command line, and it works just fine. Both accounts are on the exact same system. Each account has its own version of the script which is owned by the account. If it matters, this system is running Redhat linux 6.2. Any ideas???
# 2  
Old 01-27-2012
Code:
root@bt> cat run
#!/bin/bash
shopt -s expand_aliases
alias a='echo hello world'
a

man aliases
Quote:
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the description of
shopt under SHELL BUILTIN COMMANDS below).
man shopt
Quote:
expand_aliases
If set, aliases are expanded as described above under ALIASES. This option is enabled by default for interactive shells.
Why it was working for the other account, may be expand_aliases was set in his profile or somewhere or may be some other reason. Just my guess!

--ahamed

Last edited by ahamed101; 01-27-2012 at 01:30 PM..
# 3  
Old 01-27-2012
Thank you. You're exactly right! I've since done some additional research myself under the bash man page and rediscovered what I remember reading from about 2 years ago. I had simply forget about that option. BTW, the initial account where the script was working was my own personal account, and yes, in my login files I had already set that particular shopt option. In the account I was trying to get the script to work, that particular option had not been set.
# 4  
Old 01-27-2012

Also note from the bash man page:

For almost every purpose, aliases are superseded by shell functions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Weird issue - *ksh script not recognized when being called

HI Team - I'm running into a weird issue when trying to call a .ksh script. In my shell script, I'm using the following command to call my environment file: cd /hypbin/test ./_env.ksh But it's saying not found. Permissions are set correctly, shebang is set but I'm unsure why it's not... (5 Replies)
Discussion started by: SIMMS7400
5 Replies

2. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

3. Shell Programming and Scripting

Exiting the script if the character is not recognized

Below is the script that i'm using but i'm getting an error, echo -n "Read the letter >(enter a or b or c) " read letter if || || ; then echo "unacceptable character" else echo "Character Accepted" fi if the character entered is not equal to a or b or c, the script should... (6 Replies)
Discussion started by: web2moha
6 Replies

4. 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

5. 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

6. 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

7. 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

8. UNIX for Dummies Questions & Answers

"\n" wasn't recognized in exp script

hi, I am new in Expect I wanna write something into a file, but it didn't recognize \n in my string. for example, a simple script like this: #!/usr/local/bin/expect -- set logfile puts $logfile "===========================\n hello,allen\n===========================\n" it shown... (1 Reply)
Discussion started by: allenxiao7
1 Replies

9. 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

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