-1

Is there a way to run Linux command after the # operator for example:

testuser$ ls # pwd

To chain multiple commands?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 1
    Why do you want to use #? Just put a semicolon between them. ls ; pwd – Mike Wodarczyk Apr 11 '19 at 16:00
  • Doing it for a challenge. Would be a good learning too. – Vishal Singh Apr 11 '19 at 16:01
  • You could quote the hash. The hash could be inside a FILE block, where it forms part of the file block body. Otherwise the bash interpreter gets there before anything else. – Gem Taylor Apr 11 '19 at 16:17
  • Could you help me with an example please? – Vishal Singh Apr 11 '19 at 17:15
  • [Inline commments for Bash?](https://stackoverflow.com/q/2524367/4154375) and/or [How to put a line comment for a multi-line command](https://stackoverflow.com/q/9522631/4154375) may be relevant. – pjh Apr 11 '19 at 18:16
  • There is a bash symbol that represents the current script. You could have a statement in the script that reads the script file sends the file though a sed command that replaces # with ;. Then you can eval the result of that and exit. Of course this kind of script is callling itself (kind of) so you would have to skip the part that loads and calls itself in the read file. – Mike Wodarczyk Apr 12 '19 at 00:02
  • The moral of the story is that they if you are looking for malicious script code, then ignoring comments could be a problem if you don’t understand what the script is doing. – Mike Wodarczyk Apr 12 '19 at 00:04
  • To add to that, consider this mind bender. A script can be written with a malicious script encoded as only white space characters. The script can read itself, keep only white space type characters. It could decode the malicious script and execute it. You would never see the malicious code. – Mike Wodarczyk Apr 12 '19 at 00:14

2 Answers2

0

In bash you can split multiple commands in a few ways:

  • to run cmd1 and cmd2 unconditionally one after another you can use the semicolon: cmd1; cmd2
  • to run cmd2 only if cmd1 succeeds you can use double-and: cmd1 && cmd2

In your case I assume what you wanted to do is simply ls; pwd

w1d3m0d3
  • 1
  • 1
  • 3
-1

On the linux system, the pound symbol is used to comment text. You might want to try using &&. Example:

ls && ls -lah

This would list files and wait for the ls command to finish before running ls again with different switches. Stated another way:

&&

jmonc
  • 1
  • 1
  • 1
    The && should be used if you want the first command to be “true” ie have a truthful exit code. Yes f you always want to run the second command, just use a semicolon between them. – Mike Wodarczyk Apr 11 '19 at 16:02
  • The question is a bit confusing, based on the last comment. Are you trying to run a bash shell script with multiple commands in it? Or, are you manually running commands at the command line? If so, then would you just not type the # character and associated text? Could you provide more details? – jmonc Apr 11 '19 at 21:49