LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 12:39 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

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, 12:45 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

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

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, 01:00 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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 01:31 PM.
 
Old 01-18-2015, 01:29 PM   #5
urbanwks
Member
 
Registered: Sep 2003
Distribution: Slackware64-Current, FreeBSD 12.1, Alpine 5.4, Manjaro 19, Alpine on WSL [Win10]
Posts: 194

Rep: Reputation: 213Reputation: 213Reputation: 213
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, 01:38 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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 01:50 PM.
 
Old 01-18-2015, 02:26 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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, 05:52 PM   #8
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

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


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:12 AM.

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