I want to include it in the command

Asked 4 months ago, Updated 4 months ago, 7 views

How do I run a program I created on Linux from my device as a command?
I'm using Ubuntu
For example
It's like running a Hello World or a program with the hi command.

linux

2022-09-30 11:42

3 Answers

The ultimate goal is

1. Make the command available to all users on the same machine, then
Place the generated executable in the directory contained in the PATH environment variable
(Configure permissions if necessary)
/usr/local/bin typically placed around

2. Make the command available only to yourself, then
Add directory instructions for your personal binary to the environment variable PATH that you set. Place executable files in that directory (as well as setting permissions if necessary)
/home/alice/bin to avoid causing trouble to others

3. $hi I tried to run the program I wanted to test, but I couldn't.
Then you can supplement $./hi and directory specification

I guess so.


2022-09-30 11:42

If it's C, it's

$vitest.c

#include<stdio.h>
int main(void){
        printf("hello\n");
}

$cc test.c-ohi
$ ./hi

hello

and so on.
For sh script,

$vihi

#!/bin/bash
echo hello

$chmod+xhi
$./hi

hello

How about that?


2022-09-30 11:42

The prerequisite is to have permission to execute the file, so if necessary, set it to chmod.
Alternatively, you can specify it with the install command.

$chmod+x/hoge/foo/bin/hi
 or
$ Copy install-D-m755hi/hoge/foo/bin/hi←hi to /hoge/foo/bin/hi to grant execution rights

Answer three ways to do it.

Example) Absolute Path

Specify by absolute path from $/hoge/foo/bin/hi←"/"
Hello world!

Example) For Relative Paths

$cd/home/user/←Move origin directory
$ .../../hoge/foo/bin/hi ← Specify relative path from origin
Hello world!

$ cd/hoge/foo ← Move originating directory
$ ./bin/hi ← specified by relative path from origin
Hello world!

Example) Find and run the environment variable PATH

$echo$PATH←Try displaying the environment variable PATH
/bin: /usr/bin: /usr/local/bin←/bin, /usr/bin, and /usr/local/bin.
$ sudo install-m 755 hi/usr/local/bin/hi←hi installed in /usr/local/bin
$ hi
Hello world!

The act of allowing commands to be found in the environment variable PATH is commonly referred to as
passing. Use the export command to modify the environment variable PATH to "pass through."

 Install $install-D-m755 hi/home/user/bin/hi←hi to /home/user/bin
$ export PATH=$PATH:/home/user/bin←Add directories separated by 」: で to PATH
$ echo$PATH
/bin:/usr/bin:/usr/local/bin:/home/user/bin ← Added
$ hi
Hello world!

The export is discarded when you log out or exit the terminal.
It's troublesome to type every time, so I'll write it down in the configuration file.

$vim to /.bash_profile
For short...
export PATH=$PATH:/home/user/bin←add to configuration file


2022-09-30 11:42

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.