inheritance of a variable from source overriding my current variableBash: why isn't “set” behaving like...
Why are Tucker and Malcolm not dead?
Does fossil fuels use since 1990 account for half of all the fossil fuels used in history?
A torrent of foreign terms
Can lodestones be used to magnetize crude iron weapons?
Why is Python 2.7 still the default Python version in Ubuntu?
Chunk + Enumerate a list of digits
Installing Windows to flash BIOS, then reinstalling Ubuntu
Scam? Phone call from "Department of Social Security" asking me to call back
Can the IPA represent all languages' tones?
How is являться different from есть and быть
Graphs for which a calculus student can reasonably compute the arclength
Are those flyers about apartment purchase a scam?
Who invented Monoid?
What unique challenges/limitations will I face if I start a career as a pilot at 45 years old?
How much can I judge a company based on a phone screening?
What kind of liquid can be seen 'leaking' from the upper surface of the wing of a Boeing 737-800?
The cat exchanges places with a drawing of the cat
Do beef farmed pastures net remove carbon emissions?
Heating Margarine in Pan = loss of calories?
How can I see if the data in a SQL Server table is page-compressed?
What can Amex do if I cancel their card after using the sign up bonus miles?
Why aren't rainbows blurred-out into nothing after they are produced?
Are there any other rule mechanics that could grant Thieves' Cant?
Is there a SQL/English like language that lets you define formulations given some data?
inheritance of a variable from source overriding my current variable
Bash: why isn't “set” behaving like I expect it to?how to get a variable's definition filebash script locked at if statement when executed(Shell Script) Variable not pass correctly when fetching value from file?Run bash script from curl failed while from file succeededHow to output a shell command into a variable?How to output in columns using Bash built-in `printf` where source contains colours?Not able to replace String while reading the fileEchoing a tail command produces unexpected output?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
index.sh file:
#!/bin/sh
source "global.sh"
echo "${CODE_DIR}/SOMETHING"
global.sh file:
#!/bin/sh
APP_DIR="/app001/Documents/files/"
CODE_DIR="${APP_DIR}/code"
When I run index.sh:
Expected output: /app001/Documents/files/code/SOMETHING
output: /SOMETHINGcuments/files//code
Why? and how to resolve this issue?
bash shell-script
New contributor
add a comment |
index.sh file:
#!/bin/sh
source "global.sh"
echo "${CODE_DIR}/SOMETHING"
global.sh file:
#!/bin/sh
APP_DIR="/app001/Documents/files/"
CODE_DIR="${APP_DIR}/code"
When I run index.sh:
Expected output: /app001/Documents/files/code/SOMETHING
output: /SOMETHINGcuments/files//code
Why? and how to resolve this issue?
bash shell-script
New contributor
About the slashes you have...files/
and${APP_DIR}/
, so, two slashes together.
– guillermo chamorro
14 hours ago
About theSOMETHINGcuments
I copy/paste your code and got the expected result. There must be a typo somewhere
– guillermo chamorro
14 hours ago
3
I'm pretty sure at least one of your files has DOS/Windows line endings -- see this SO question.
– Gordon Davisson
14 hours ago
With your files as shown in the question on my Ubuntu 19.04 I get an error message./index.sh: 2: ./index.sh: source: not found
followed by/SOMETHING
becausesource
is not valid POSIX shell syntax. After changingindex.sh
to#!/bin/bash
I get/app001/Documents/files//code/SOMETHING
– Bodo
13 hours ago
add a comment |
index.sh file:
#!/bin/sh
source "global.sh"
echo "${CODE_DIR}/SOMETHING"
global.sh file:
#!/bin/sh
APP_DIR="/app001/Documents/files/"
CODE_DIR="${APP_DIR}/code"
When I run index.sh:
Expected output: /app001/Documents/files/code/SOMETHING
output: /SOMETHINGcuments/files//code
Why? and how to resolve this issue?
bash shell-script
New contributor
index.sh file:
#!/bin/sh
source "global.sh"
echo "${CODE_DIR}/SOMETHING"
global.sh file:
#!/bin/sh
APP_DIR="/app001/Documents/files/"
CODE_DIR="${APP_DIR}/code"
When I run index.sh:
Expected output: /app001/Documents/files/code/SOMETHING
output: /SOMETHINGcuments/files//code
Why? and how to resolve this issue?
bash shell-script
bash shell-script
New contributor
New contributor
New contributor
asked 14 hours ago
shnisakashnisaka
1033 bronze badges
1033 bronze badges
New contributor
New contributor
About the slashes you have...files/
and${APP_DIR}/
, so, two slashes together.
– guillermo chamorro
14 hours ago
About theSOMETHINGcuments
I copy/paste your code and got the expected result. There must be a typo somewhere
– guillermo chamorro
14 hours ago
3
I'm pretty sure at least one of your files has DOS/Windows line endings -- see this SO question.
– Gordon Davisson
14 hours ago
With your files as shown in the question on my Ubuntu 19.04 I get an error message./index.sh: 2: ./index.sh: source: not found
followed by/SOMETHING
becausesource
is not valid POSIX shell syntax. After changingindex.sh
to#!/bin/bash
I get/app001/Documents/files//code/SOMETHING
– Bodo
13 hours ago
add a comment |
About the slashes you have...files/
and${APP_DIR}/
, so, two slashes together.
– guillermo chamorro
14 hours ago
About theSOMETHINGcuments
I copy/paste your code and got the expected result. There must be a typo somewhere
– guillermo chamorro
14 hours ago
3
I'm pretty sure at least one of your files has DOS/Windows line endings -- see this SO question.
– Gordon Davisson
14 hours ago
With your files as shown in the question on my Ubuntu 19.04 I get an error message./index.sh: 2: ./index.sh: source: not found
followed by/SOMETHING
becausesource
is not valid POSIX shell syntax. After changingindex.sh
to#!/bin/bash
I get/app001/Documents/files//code/SOMETHING
– Bodo
13 hours ago
About the slashes you have
...files/
and ${APP_DIR}/
, so, two slashes together.– guillermo chamorro
14 hours ago
About the slashes you have
...files/
and ${APP_DIR}/
, so, two slashes together.– guillermo chamorro
14 hours ago
About the
SOMETHINGcuments
I copy/paste your code and got the expected result. There must be a typo somewhere– guillermo chamorro
14 hours ago
About the
SOMETHINGcuments
I copy/paste your code and got the expected result. There must be a typo somewhere– guillermo chamorro
14 hours ago
3
3
I'm pretty sure at least one of your files has DOS/Windows line endings -- see this SO question.
– Gordon Davisson
14 hours ago
I'm pretty sure at least one of your files has DOS/Windows line endings -- see this SO question.
– Gordon Davisson
14 hours ago
With your files as shown in the question on my Ubuntu 19.04 I get an error message
./index.sh: 2: ./index.sh: source: not found
followed by /SOMETHING
because source
is not valid POSIX shell syntax. After changing index.sh
to #!/bin/bash
I get /app001/Documents/files//code/SOMETHING
– Bodo
13 hours ago
With your files as shown in the question on my Ubuntu 19.04 I get an error message
./index.sh: 2: ./index.sh: source: not found
followed by /SOMETHING
because source
is not valid POSIX shell syntax. After changing index.sh
to #!/bin/bash
I get /app001/Documents/files//code/SOMETHING
– Bodo
13 hours ago
add a comment |
1 Answer
1
active
oldest
votes
This is happening because your global.sh
has DOS file format, which encodes end of lines as a carriage return followed by a newline character.
Linux/Unix will only consider the newline character to be a newline, so the carriage return will become part of the contents of the variable ${CODE_DIR}
.
When you echo a carriage return character, the cursor goes back to the beginning of the line. That's what's happening in this case, first it's printing /app001/Documents/files//code
from ${CODE_DIR}
, then the carriage return at the end of that variable will move the cursor back to the beginning of the line, and then the /SOMETHING
part will overwrite the start of the string.
You can visualize that by piping the output through cat -v
which will show all characters, it will show the carriage return as ^M
:
$ ./index.sh | cat -v
/app001/Documents/files//code^M/SOMETHING
In order to fix it, convert the newlines on global.sh
to Unix format, which you can do using the dos2unix
tool, if available on your machine.
$ dos2unix global.sh
Or open the file in Vim and convert it there:
$ vim global.sh
:set ff=dos
:wq
You might want to check your other files (such as index.sh
) for DOS line endings. You might also want to check why global.sh
ended up with DOS file endings and fix that. If you're using a text editor to create these files, check that the text editor is properly configured to always use Unix line endings.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
shnisaka is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f535407%2finheritance-of-a-variable-from-source-overriding-my-current-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is happening because your global.sh
has DOS file format, which encodes end of lines as a carriage return followed by a newline character.
Linux/Unix will only consider the newline character to be a newline, so the carriage return will become part of the contents of the variable ${CODE_DIR}
.
When you echo a carriage return character, the cursor goes back to the beginning of the line. That's what's happening in this case, first it's printing /app001/Documents/files//code
from ${CODE_DIR}
, then the carriage return at the end of that variable will move the cursor back to the beginning of the line, and then the /SOMETHING
part will overwrite the start of the string.
You can visualize that by piping the output through cat -v
which will show all characters, it will show the carriage return as ^M
:
$ ./index.sh | cat -v
/app001/Documents/files//code^M/SOMETHING
In order to fix it, convert the newlines on global.sh
to Unix format, which you can do using the dos2unix
tool, if available on your machine.
$ dos2unix global.sh
Or open the file in Vim and convert it there:
$ vim global.sh
:set ff=dos
:wq
You might want to check your other files (such as index.sh
) for DOS line endings. You might also want to check why global.sh
ended up with DOS file endings and fix that. If you're using a text editor to create these files, check that the text editor is properly configured to always use Unix line endings.
add a comment |
This is happening because your global.sh
has DOS file format, which encodes end of lines as a carriage return followed by a newline character.
Linux/Unix will only consider the newline character to be a newline, so the carriage return will become part of the contents of the variable ${CODE_DIR}
.
When you echo a carriage return character, the cursor goes back to the beginning of the line. That's what's happening in this case, first it's printing /app001/Documents/files//code
from ${CODE_DIR}
, then the carriage return at the end of that variable will move the cursor back to the beginning of the line, and then the /SOMETHING
part will overwrite the start of the string.
You can visualize that by piping the output through cat -v
which will show all characters, it will show the carriage return as ^M
:
$ ./index.sh | cat -v
/app001/Documents/files//code^M/SOMETHING
In order to fix it, convert the newlines on global.sh
to Unix format, which you can do using the dos2unix
tool, if available on your machine.
$ dos2unix global.sh
Or open the file in Vim and convert it there:
$ vim global.sh
:set ff=dos
:wq
You might want to check your other files (such as index.sh
) for DOS line endings. You might also want to check why global.sh
ended up with DOS file endings and fix that. If you're using a text editor to create these files, check that the text editor is properly configured to always use Unix line endings.
add a comment |
This is happening because your global.sh
has DOS file format, which encodes end of lines as a carriage return followed by a newline character.
Linux/Unix will only consider the newline character to be a newline, so the carriage return will become part of the contents of the variable ${CODE_DIR}
.
When you echo a carriage return character, the cursor goes back to the beginning of the line. That's what's happening in this case, first it's printing /app001/Documents/files//code
from ${CODE_DIR}
, then the carriage return at the end of that variable will move the cursor back to the beginning of the line, and then the /SOMETHING
part will overwrite the start of the string.
You can visualize that by piping the output through cat -v
which will show all characters, it will show the carriage return as ^M
:
$ ./index.sh | cat -v
/app001/Documents/files//code^M/SOMETHING
In order to fix it, convert the newlines on global.sh
to Unix format, which you can do using the dos2unix
tool, if available on your machine.
$ dos2unix global.sh
Or open the file in Vim and convert it there:
$ vim global.sh
:set ff=dos
:wq
You might want to check your other files (such as index.sh
) for DOS line endings. You might also want to check why global.sh
ended up with DOS file endings and fix that. If you're using a text editor to create these files, check that the text editor is properly configured to always use Unix line endings.
This is happening because your global.sh
has DOS file format, which encodes end of lines as a carriage return followed by a newline character.
Linux/Unix will only consider the newline character to be a newline, so the carriage return will become part of the contents of the variable ${CODE_DIR}
.
When you echo a carriage return character, the cursor goes back to the beginning of the line. That's what's happening in this case, first it's printing /app001/Documents/files//code
from ${CODE_DIR}
, then the carriage return at the end of that variable will move the cursor back to the beginning of the line, and then the /SOMETHING
part will overwrite the start of the string.
You can visualize that by piping the output through cat -v
which will show all characters, it will show the carriage return as ^M
:
$ ./index.sh | cat -v
/app001/Documents/files//code^M/SOMETHING
In order to fix it, convert the newlines on global.sh
to Unix format, which you can do using the dos2unix
tool, if available on your machine.
$ dos2unix global.sh
Or open the file in Vim and convert it there:
$ vim global.sh
:set ff=dos
:wq
You might want to check your other files (such as index.sh
) for DOS line endings. You might also want to check why global.sh
ended up with DOS file endings and fix that. If you're using a text editor to create these files, check that the text editor is properly configured to always use Unix line endings.
answered 13 hours ago
filbrandenfilbranden
13.4k2 gold badges27 silver badges56 bronze badges
13.4k2 gold badges27 silver badges56 bronze badges
add a comment |
add a comment |
shnisaka is a new contributor. Be nice, and check out our Code of Conduct.
shnisaka is a new contributor. Be nice, and check out our Code of Conduct.
shnisaka is a new contributor. Be nice, and check out our Code of Conduct.
shnisaka is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f535407%2finheritance-of-a-variable-from-source-overriding-my-current-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
About the slashes you have
...files/
and${APP_DIR}/
, so, two slashes together.– guillermo chamorro
14 hours ago
About the
SOMETHINGcuments
I copy/paste your code and got the expected result. There must be a typo somewhere– guillermo chamorro
14 hours ago
3
I'm pretty sure at least one of your files has DOS/Windows line endings -- see this SO question.
– Gordon Davisson
14 hours ago
With your files as shown in the question on my Ubuntu 19.04 I get an error message
./index.sh: 2: ./index.sh: source: not found
followed by/SOMETHING
becausesource
is not valid POSIX shell syntax. After changingindex.sh
to#!/bin/bash
I get/app001/Documents/files//code/SOMETHING
– Bodo
13 hours ago