Find the answer to your Linux question:
Results 1 to 4 of 4
Hey everyone! So, I'm trying to create and write to a file with a script. I know how to create the file with touch. I know how to write to ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    9

    Script to create file

    Hey everyone!

    So, I'm trying to create and write to a file with a script.

    I know how to create the file with touch.
    I know how to write to the file with echo and >> / >

    but, I'm having problems writing special characters, in particular, "$" and a quotation mark.

    So, when I put this line in my script...
    echo "case "$1" in" >> file.txt
    it obviously doesn't write it out correctly, as it won't write the $ or the " mark.

    Also, when I do...
    echo "cd $JAVA_HOME/bin/" >> file.txt
    it doesn't correctly write out my variable, as I assume it is trying to replace it with the variable value. Is there a way I can write the $ into another file with my script?

    I know it's probably a relatively easy question, but I've searched everywhere on the internet and can't seem to find anything about it.



    Nevermind, it's just \$ or \". Bah, took me way too long to figure that out!

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    What is more, you can use apostrophes ('), which keep the literal meaning of anything they embrace. They prevent variable expansion for example.

    Code:
    system:~$ echo "$HOME"
    /home/gnufan
    
    system:~$ echo '$HOME'
    $HOME
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Posts
    9
    Okay, now I have a legitimate question, how do you copy the contents of a file and append them to the contents of another file?

  4. #4
    Just Joined!
    Join Date
    Aug 2009
    Posts
    9
    Nevermind, apparently you can copy contents into another file with cat and >>.

    Thanks for the tip GNU-Fan, I'm going to use your idea as that looks a lot prettier and much more intuitive.

    I'll try to post some GOOD questions sometime soon.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...