Execute Alias inside script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute Alias inside script
# 1  
Old 01-10-2014
Power Execute Alias inside script

Hi,

in the .profile i have

Code:
alias jh="cd /var/jdk"

What should i write in my unix shell script to use jh and move to /var/jdk directory ?

I tried the following in myscript.sh
Code:
jh
$jh
$(jh)

but none works !!

Last edited by mohtashims; 01-10-2014 at 04:12 PM..
# 2  
Old 01-10-2014
Macbook Pro 13 inch, OSX 10.7.5 default bash terminal...
Code:
#!/bin/sh
alias jh="cd /usr/bin"
jh
printf "Whilst you are in this sub-shell you are here:- "
pwd
echo "Until you exit to the command line again..."
exit 0

Results:-
Code:
Last login: Fri Jan 10 21:01:43 on ttys000
AMIGA:barrywalker~> chmod 755 aliasit.sh
AMIGA:barrywalker~> ./aliasit.sh
Whilst you are in this sub-shell you are here:- /usr/bin
Until you exit to the command line again...
AMIGA:barrywalker~> _

# 3  
Old 01-10-2014
Aliases are meant for interactive use and are ignored for scripts. This is because aliases are often used to override commands, i.e. aliasing "rm" to "rm -i" for root to make it extra-paranoid. But you wouldn't want automatic things to be run with -i.

If you want something a script can use, how about a function?

Code:
jh() {
        cd "/usr/bin"
}

These work fine in both scripts and logins.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute a command inside a while loop?

How do we execute a command inside a while loop? (7 Replies)
Discussion started by: Little
7 Replies

2. Shell Programming and Scripting

Alias has no effect in script

It doesn't have effect in script but it works on the terminal 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" ... (3 Replies)
Discussion started by: yanglei_fage
3 Replies

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

4. Shell Programming and Scripting

how do i execute nohup and background job inside the korn script for db2

load_cursor_stmt() { ls /db/admin/ddl/rmn01000/load_cursor*.sql|while read file do echo "${file}" `nohup db2 -tvf "$file" \&` done }Error: ------- /admin/ddl/rmn01000/load_cursor5.sql /db/admin/ddl/rmn01000/load_cursor6.sql + read file + echo... (3 Replies)
Discussion started by: Hangman2
3 Replies

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

6. UNIX for Dummies Questions & Answers

how to use expect - send to execute list of commands inside a file

:)hello people i am working on some kind of PBX and i have list of telephone numbers inside a file, i have to insert these numbers into the correct command and then telnet to a remote server and execute these commands. i can read the telephone numbers and insert them into the command with no... (0 Replies)
Discussion started by: auma78
0 Replies

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

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

9. Shell Programming and Scripting

Execute command inside while problem

Hi! How can I execute a linux command inside the while cicle?? like: This doesn't work. Should I replace the by '' or "" (3 Replies)
Discussion started by: ruben.rodrigues
3 Replies

10. Shell Programming and Scripting

script execute or no execute

o hola.. Tengo un script que se ejecuta bajo una tarea del CronJOb del unix, tengo la version 11 de unix, mi script tiene un ciclo que lee unos archivos .txt luego cada uno de esos archivos debe pasar por un procedimiento almacenado el cual lo tengo almacenado en mi base de datos oracle 10g,... (4 Replies)
Discussion started by: Kespinoza97
4 Replies
Login or Register to Ask a Question