in linux ubuntu, i want to create an executable from 2 lines of code (that i always use…
- User
in linux ubuntu, i want to create an executable from 2 lines of code (that i always use on terminal). how do i do it?
- Assistant
Creating an executable script from two lines of code that you regularly use in the terminal on Ubuntu (or any Linux distribution) can be done by creating a shell script. Here’s a step-by-step guide:
-
Open a text editor: You can use any text editor like
nano,gedit, orvim. For simplicity, we’ll usenano. -
Create the script file: Open your terminal and type:
nano myscript.sh -
Write your commands: In the
nanoeditor, type in the two lines of code you regularly use. For example, if your commands are:echo "Hello, World!" ls -laYour
myscript.shfile should look like this:#!/bin/bash echo "Hello, World!" ls -laThe
#!/bin/bashat the top is called a shebang and it tells the system to use the Bash shell to execute the script. -
Save and exit: To save and exit
nano, pressCTRL + O(to write out the file), hitEnterto confirm, and then pressCTRL + Xto exit the editor. -
Make the script executable: Change the script’s permissions to make it executable:
chmod +x myscript.sh -
Run your script: You can now run your script by typing:
./myscript.shAlternatively, if you want to run your script from anywhere without specifying the path, you can move it to a directory that is in your PATH, such as
/usr/local/bin:sudo mv myscript.sh /usr/local/bin/myscriptYou can now run your script simply by typing:
myscript
Make sure to replace the placeholder commands with your actual two lines of code. That’s it! You’ve created an executable script containing your two terminal commands.
-