LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-18-2015, 01:39 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,367

Rep: Reputation: 55
Is there a way to have a script called source, that can be run without the full path?


Is there a way to have a script called source, that can be run without the full path?

Ex:
When I run:
export PATH=/home/user:$PATH
source

I get:
Code:
$ source
bash: source: filename argument required
source: usage: source filename [arguments]
However I want it to run my script called source:
Code:
$ cat source 
#!/bin/bash
echo "Hello World"
Is there way to override a shell builtin in the PATH var or some other way?
 
Old 01-18-2015, 01:45 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,958
Blog Entries: 1

Rep: Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059
alias should do it.
 
1 members found this post helpful.
Old 01-18-2015, 01:53 PM   #3
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,367

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by astrogeek View Post
alias should do it.
Thanks! That worked:
Code:
$ alias source="~/source"
$ source
Hello World
But, how can I get that to carry over to a script though?

Ex.
Code:
$ ./source2
./source2: line 2: source: filename argument required
source: usage: source filename [arguments]
[biosadm@nickbios ~]$ cat source2
#!/bin/bash
source
 
Old 01-18-2015, 02:00 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,958
Blog Entries: 1

Rep: Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059
Quote:
Originally Posted by abefroman View Post
Thanks! That worked:
Code:
$ alias source="~/source"
$ source
Hello World
But, how can I get that to carry over to a script though?

Ex.
Code:
$ ./source2
./source2: line 2: source: filename argument required
source: usage: source filename [arguments]
[biosadm@nickbios ~]$ cat source2
#!/bin/bash
source
OK, if I understand you correctly, what you want is for the fake-souce to be be available to non-interactive shells (i.e. when running a script).

To do that you would need the alias to be set through the BASH_ENV variable.

From man bash:

Quote:
When bash is started non-interactively, to run a shell script, for example, it looks for the variable
BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the
name of a file to read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the filename.

Last edited by astrogeek; 01-18-2015 at 02:31 PM.
 
Old 01-18-2015, 02:29 PM   #5
urbanwks
Member
 
Registered: Sep 2003
Distribution: Slackware64-Current, FreeBSD 11.0
Posts: 110

Rep: Reputation: 104Reputation: 104
if you mean that you want to run

Code:
source
instead of

Code:
/home/user/source
then you can put (or link) "source" in /bin or /usr/bin or /usr/local/bin. they should already be in your path.

or maybe i misunderstand.
 
Old 01-18-2015, 02:38 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,958
Blog Entries: 1

Rep: Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059Reputation: 2059
Quote:
Originally Posted by urbanwks View Post
if you mean that you want to run

Code:
source
instead of

Code:
/home/user/source
then you can put (or link) "source" in /bin or /usr/bin or /usr/local/bin. they should already be in your path.

or maybe i misunderstand.
For source, there is no binary on many/most systems, it is a bash built-in, which I think is the OPs point.

An alias can override a built-in, but then the alias must be set in the non-interactive shell context as well.

But I am curious - why override the built-in for source? It seems like a good way to break a system with unanticipated side effects!

And then what about ., should it act the same way or be different?

Last edited by astrogeek; 01-18-2015 at 02:50 PM.
 
Old 01-18-2015, 03:26 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,055

Rep: Reputation: 1319Reputation: 1319Reputation: 1319Reputation: 1319Reputation: 1319Reputation: 1319Reputation: 1319Reputation: 1319Reputation: 1319Reputation: 1319
By default, aliases are not expanded in non-interactive shells. You can use the shopt builtin command to set the expand_aliases option, but that puts your shell at the mercy of whatever aliases happen to have been set. Unless you are running in a tightly controlled environment, there would be no command you could trust unless you have carefully purged the BASH_ALIASES associative array of anything not wanted.
 
2 members found this post helpful.
Old 01-18-2015, 06:52 PM   #8
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,367

Original Poster
Rep: Reputation: 55
Thanks for the info on this!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Installing an extra version of CMake - can run from full path but not from $PATH mattachu Linux - Software 1 05-23-2014 04:58 PM
Allow a shell script to run only once if called multiple times. Springs Linux - General 1 03-27-2013 08:35 AM
script works when run but not when called by cron grob115 Linux - Server 7 08-28-2010 12:24 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
Run any Script without metioning the full path shipon_97 Linux - Newbie 2 08-08-2006 11:49 PM


All times are GMT -5. The time now is 10:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration