0

So, I want to make a factorial command for the linux terminal, and I want to be able to run it like this:

$ factorial 5
120

However, when I look at questions about this, I only see commands that runs a SET of commands, like for example a command that changes your directory to ~/Downloads and echoes "Went to Downloads!"

How can I make the factorial function as a command, and if this is a duplicate, could you at least tell me why it is a duplicate?

5
  • 1
    Some shell scripts just run a list of commands. But shell also has many built-in commands of its own, to do arithmetic, make loop constructs, and format text. For factorial 5, you would just want a for loop that contained a multiply operation, and counted up to 5. Jan 27 at 23:42
  • Alright, the thing is, in what type of programming language would I do so? Jan 27 at 23:53
  • what language do you know? Use that - but since you said you want to "make a script", assuming you meant shell script, then that's what language you would use Jan 27 at 23:56
  • Python. That's basically it, although I wouldn't be inconvenienced to learn shell script. Jan 27 at 23:57
  • "Alright, the thing is, in what type of programming language would I do so?" – Any programming language you want. If you know any programming language at all, this should be a one-line problem. Jan 28 at 12:40

1 Answer 1

0

This is future me:

  • Add a bin directory if you haven't already in your home directory by running mkdir ~/bin [Cited]
  • Afterwards, make a python file that does etc.
  • Then, use cd to go to your bin directory.
  • Then, run export PATH=$PATH":$HOME/bin" (Straight from an answer from cited source)
  • Afterwards, make a file in this format:
#!/bin/bash
python3 [path to python file]
  • Afterward, run chmod +x [bin file name goes here] [Cited]
  • Finally, run . ~/.bash_profile (Again, straight from citation)

Now, you have made a command that will run the python file.

I do have to cite this Linux-Unix stack exchange question, as I got most of the information here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .