0

After editing / copying the original bash script which was in colors I now have plain black / white nano editor text in the file. It's no biggie, just like to find out how it possibly happen and if I can "recover" back to color text. Thanks

0

2 Answers 2

4

nano uses file extensions and shebang (#!) lines to determine how to highlight a file.

Save the file in nano with the .sh extension as well as having #!/bin/bash in the first line of the file/script and it'll autoapply the syntax highlighting.

Also running nano filename.sh would have auto-enabled the highlighting as well, since it knows the filename you're going to be writing to.

4
  • 1
    I did have #!/bin/bash, but adding .sh extension solved the problem .
    – user580382
    Aug 7, 2018 at 16:02
  • @ThomasWard this is not strictly correct you can enable highlighting by simply specifying a file name on launching nano nano scripname.sh would do the trick. Aug 7, 2018 at 17:52
  • @MattBashton you're right, but OP had indicated they just ran nano and pasted, or at least that's how i interpreted it.
    – Thomas Ward
    Aug 7, 2018 at 20:20
  • Yes that bit of the question could do with editing for clarity, was hoping the OP would do this! Aug 7, 2018 at 20:31
1

Can you explain a bit more about the source of your script, was it copied from another editor or webpage, was it in colour there?

The colour is normally as a result of syntax highlighting which is not a property of the text it's self it's just pain text, the highlighting occurs because the text editor of choice has a system which understands and highlights specific words / characters according to the category of item, e.g. variable, function etc. for the programming language being used. This is often detected via file extension e.g. .sh for a shell/bash script and or the initial shebang line e.g. #!/bin/bash which would indicate that the interpreter is the bash binary in the /bin directory, which should cause an editor to use syntax highlighting appropriate for the bash shell language.

It's not strictly true that you need to save the file first before syntax highlighting will work, this is only correct if you run nano without specifying a file name to write to, so if you type nano scriptname.sh it will highlight right away before you've written out the file. In this case it has detected the language from your the .sh file extension.

If you're having issue with syntax highlighting in nano it can be enabled by editing your ~/.nanorc configuration file as explained here, with further relevant documentation available on the GNU Nano homepage.

7
  • 2
    nano syntax highlighting only takes place on open or on file save, if you have a brand new file, it needs to be saved as such.
    – Thomas Ward
    Aug 7, 2018 at 15:21
  • I would not post the question if it was not in color when first edited. Yes, I did change the file name.
    – user580382
    Aug 7, 2018 at 15:52
  • This answer has some good links, but it would be great if you could edit to include the key takeaways from those links in your answer. The last line of your answer seems to be what the OP was looking for?
    – Tim
    Aug 7, 2018 at 16:03
  • @ThomasWard this is not strictly correct you can enable highlighting by simply specifying a file name on launching nano nano scripname.sh would do the trick. Aug 7, 2018 at 17:52
  • @Tim thanks for suggestions have added in additional comments. Aug 7, 2018 at 17:54

You must log in to answer this question.