Bash: Including a path from one file into another

  • Thread starter Thread starter jf22901
  • Start date Start date
  • Tags Tags
    File Path
Click For Summary

Discussion Overview

The discussion revolves around how to effectively include a specified path from one file into another in bash scripting. Participants explore various methods to manage directory paths in scripts, aiming for a solution that minimizes manual updates across multiple files.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant suggests creating a file named 'path.sh' to define the path variable and using the 'source' command to include it in other scripts, but encounters issues with the syntax.
  • Another participant proposes using the 'export' command to make the path variable available to other scripts, indicating that the simple 'set' command may not persist beyond the script's runtime.
  • A different participant offers alternatives, such as passing the path as an argument to a master script or using an environment variable, suggesting flexibility in managing script execution.
  • One participant reports success by changing the syntax from $(FILEPATH) to $FILEPATH, indicating a resolution to their initial problem while remaining open to exploring other methods.
  • Another suggestion is made to use FILEPATH=$0, although it is unclear how this fits into the broader context of the discussion.

Areas of Agreement / Disagreement

There is no clear consensus on the best approach, as participants propose multiple methods and express varying levels of success with different solutions. The discussion remains unresolved regarding the optimal strategy for including paths in bash scripts.

Contextual Notes

Participants express uncertainty about the effectiveness of different commands and methods, indicating that some approaches may depend on specific use cases or configurations. The discussion reflects a range of experiences and familiarity with bash scripting.

Who May Find This Useful

This discussion may be useful for individuals learning bash scripting, particularly those looking for efficient ways to manage file paths across multiple scripts.

jf22901
Messages
55
Reaction score
1
Hi everyone

I'm having trouble working out how to import a specified path into a bash script. Basically, I have numerous scripts, each of which writes their output to a certain directory via the command

Code:
set wdir = "/home/..."

However, in the future this directory may change, so rather than having to manually change the path of the directories in each file, I was wanting to create one file that specifies the path, and then have the other files include it. That way I only have to change one file if the path changes.

Initially I thought of having the path specified in a file such as 'path.sh', which would simply look like

Code:
FILEPATH="/home/..."

and then I thought that in the other files that require the path I could just put

Code:
#!/bin/bash
.
.
.
source path.sh
set wdir = $(FILEPATH)
.
.
.

However, such coding does not work, and I have no idea how to go about fixing it. Could anyone offer any suggestions as to the best way to include a path specified in another file? I'm new to all this bash scripting stuff! :-p

Many thanks
 
Technology news on Phys.org
try: export FILEPATH="/home/..."

I think the simple set command is only effective for the actual script runtime. I'd have to fiddle with it to prove it, especially when source-ing the file though.
 
Have you considered the option of making the path an argument?

How about using an environment variable?

How about a master script that knows the path, and you select which individual script you want to run as an argument to the master one?



If you are set on this approach, you can use "export" as schip mentioned. Alternatively, you could write a script that writes the filename to stdout and use something like

wdir = `path.sh`
 
Thanks for the replies everyone :cool:

I tried putting 'export' into the file, but it still wouldn't work. However, if I leave everything as it is but change $(FILEPATH) to $FILEPATH, it works. I'll stick with this for now, but will look into the other methods suggested.

Thanks once again
 
Use
FILEPATH=$0
Thats all you need.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
7K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
4
Views
5K
Replies
9
Views
4K
  • · Replies 12 ·
Replies
12
Views
11K
Replies
35
Views
8K