1

In a bash script I want to do that but it doesn't work

config.txt :

newdir="build/src"

script.sh :

#!/bin/bash


source config.txt
cd $newdir

: No file or folder of this type

I have this with bash +x script.sh

+ source config.txt
++ newdir=$'build/src\r'
+ cd $'build/src\r'
: Aucun fichier de ce type
Loan
  • 142
  • 1
  • 16
  • 2
    bash is *Case-Sensitive*.... and `source` is a keyword in bash you should use another variable name, e.g. `newdir="build/src"` then `source config.txt` and `cd "$newdir"`. – David C. Rankin Mar 08 '19 at 09:02
  • it doesn't work too with an other name – Loan Mar 08 '19 at 09:44
  • 1
    What you call the variable make no difference at all -- you just want a name that doesn't conflict with a reserved word. Do this run your `script.sh` as `bash -x script.sh` and post the output to tell us where it is failing. Just add it to your question (with each line indented by 4-spaces so it formats correctly) and that will tell us exactly what is going on. You still have `source config.txt` unchanged - right? And the `build/src` directory is in the current working directory when you run the script right? The `config.txt` file is also in that same directory too? – David C. Rankin Mar 08 '19 at 09:50
  • 2
    `\r` by the end of your file path is part of a Window's linefeed, you will want to remove it (retype the path by hand). The error appear as an empty filename because `\r` is the carriage return char which makes the cursor go back to the start of the line, where `: No file or folder of this type` will overwrite the file name – Aaron Mar 08 '19 at 10:13
  • your path is relative, so, when you run this script, you should be in folder that contains build/src or change it and using an absolute path – Lety Mar 08 '19 at 10:17
  • How can I remove this (I'm on notepad ++ encoded in UTF-8) ? – Loan Mar 08 '19 at 10:17
  • Actually I think the config.txt file has the following content : `newdir="build/src"\r\n`, so retyping the path only won't solve your problem, you either need to delete the invisible char that precedes the end of the line, use a tool that will remove all `\r` (dos2unix if available, tr or sed otherwise) or rewrite the whole file – Aaron Mar 08 '19 at 10:18
  • 1
    With Notepad++ use `Edit` -> `EOL Conversion` -> `Unix (LF)` in the menus ; you will probably see that the `Windows (CR LF)` menu item is grayed since that's your current linefeeds style (or in the status bar at the bottom of the window you can see "Windows (CR LF)" as the third item from the right) – Aaron Mar 08 '19 at 10:18
  • Thanks Aaron, it works well ! – Loan Mar 08 '19 at 10:20
  • Glad to hear it ! – Aaron Mar 08 '19 at 10:21
  • Possible duplicate of [Shell script change directory with variable](https://stackoverflow.com/questions/19281328/shell-script-change-directory-with-variable) – kvantour Mar 12 '19 at 14:46

1 Answers1

3

The problem came from invsible \r (carriage return) generated by windows at the end of lines. I change the EOL Conversion to Unix (LF)!

Loan
  • 142
  • 1
  • 16