LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2015, 03:18 PM   #1
NeoLux
LQ Newbie
 
Registered: Dec 2012
Posts: 15

Rep: Reputation: Disabled
sourced script won't execute alias after command expansion


Hi... So I get that you have to source a shell script to execute any currently defined aliases within it, effectively executing commands as if you had typed them yourself, but well, instead of explaining I'll just show you...

Code:
 
:> alias hi="echo hello"
:> touch test.sh
:> chmod 777 test.sh # *write some code
:> source ./test.sh
hello
hi DAD
bash: hi: command not found
---- test.sh ----
Code:
#!/bin/bash

hi
echo $(echo "hi MOM" | sed "s:MOM:DAD:")
$(echo "hi MOM" | sed "s:MOM:DAD:")
so yeah, I considered maybe it's another layer of being a "subshell" in those
$()'s, but if that were the case how would you source an expanded command?
(sorry if I'm misinterpreting the issue)
Basically, is there a workaround ?
thx to anyone who looks and gives me a reply.
-cheers & God speed !

Last edited by NeoLux; 03-07-2015 at 03:19 PM.
 
Old 03-07-2015, 04:35 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
Not sure what you are doing. Does this help?
Code:
#! /usr/bin/env bash

hi="hello"

echo ""$hi""
echo "hi"

echo ""$hi" MOM" | sed "s:MOM:DAD:"
echo "hi MOM" | sed "s:MOM:DAD:"
 
Old 03-07-2015, 05:07 PM   #3
NeoLux
LQ Newbie
 
Registered: Dec 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
no, this is specificall something with aliases but thank you for the reply all the same

I was looking and I think maybe the answer for me is "eval"

Code:
:> alias hi="echo hello"
:> hi
hello
:> echo $(echo hi)
hi
:> $(echo hi)
bash: hi: command not found
:> eval $(echo hi)
hello
That's what I was trying to do, I wanted to executed it after I had expanded it to the real command.

in this example it doesn't change anything, but for what I was doing I was expanding it to a new command that I didn't know in advance.
 
Old 03-07-2015, 08:32 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
If you use a function instead of an alias it works:
Code:
~/tmp$ cat test.sh 
#!/bin/bash

hi
echo $(echo "hi MOM" | sed "s:MOM:DAD:")
$(echo "hi MOM" | sed "s:MOM:DAD:")

hi-fun
echo $(echo "hi-fun MOM" | sed "s:MOM:DAD:")
$(echo "hi-fun MOM" | sed "s:MOM:DAD:")
~/tmp$ alias hi='echo hello'
~/tmp$ hi-fun() { echo hello "$@" ; }
~/tmp$ source test.sh 
hello
hi DAD
bash: hi: command not found
hello
hi-fun DAD
hello DAD
 
1 members found this post helpful.
Old 03-07-2015, 11:05 PM   #5
NeoLux
LQ Newbie
 
Registered: Dec 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thx, although my concern was for making this work with an alias, it'll be nice to keep the function alternative in mind.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script won't execute jazzo Linux - Newbie 5 10-04-2014 08:12 AM
[SOLVED] alias and shopt won't work in script porphyry5 Programming 9 07-07-2011 02:23 AM
Crontab - command won't execute laki47 Linux - Newbie 5 02-10-2009 03:57 AM
Calling alias command in script snowman81 Linux - Newbie 4 04-06-2008 02:39 PM
Firewall Script Won't Execute jimminy34 Linux - Security 3 02-02-2003 06:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration