1 | # Command Line Interface (CLI)
|
2 |
|
3 | When math.js is installed globally using npm, its expression parser can be used
|
4 | from the command line. To install math.js globally:
|
5 |
|
6 | ```bash
|
7 | $ npm install -g mathjs
|
8 | ```
|
9 |
|
10 | Normally, a global installation must be run with admin rights (precede the
|
11 | command with `sudo`). After installation, the application `mathjs` is available
|
12 | via the command line:
|
13 |
|
14 | ```bash
|
15 | $ mathjs
|
16 | > 12 / (2.3 + 0.7)
|
17 | 4
|
18 | > 12.7 cm to inch
|
19 | 5 inch
|
20 | > sin(45 deg) ^ 2
|
21 | 0.5
|
22 | > 9 / 3 + 2i
|
23 | 3 + 2i
|
24 | > det([-1, 2; 3, 1])
|
25 | -7
|
26 | ```
|
27 |
|
28 | The command line interface can be used to open a prompt, to execute a script,
|
29 | or to pipe input and output streams:
|
30 |
|
31 | ```bash
|
32 | $ mathjs # Open a command prompt
|
33 | $ mathjs script.txt # Run a script file, output to console
|
34 | $ mathjs script1.txt script2.txt # Run two script files
|
35 | $ mathjs script.txt > results.txt # Run a script file, output to file
|
36 | $ cat script.txt | mathjs # Run input stream, output to console
|
37 | $ cat script.txt | mathjs > results.txt # Run input stream, output to file
|
38 | ```
|
39 |
|
40 | You can also use it to create LaTeX from or sanitize your expressions using the
|
41 | `--tex` and `--string` options:
|
42 |
|
43 | ```bash
|
44 | $ mathjs --tex
|
45 | > 1/2
|
46 | \frac{1}{2}
|
47 | ```
|
48 |
|
49 | ```bash
|
50 | $ mathjs --string
|
51 | > (1+1+1)
|
52 | (1 + 1 + 1)
|
53 | ```
|
54 |
|
55 | To change the parenthesis option use the `--parenthesis=` flag:
|
56 |
|
57 | ```bash
|
58 | $ mathjs --string --parenthesis=auto
|
59 | > (1+1+1)
|
60 | 1 + 1 + 1
|
61 | ```
|
62 |
|
63 | ```bash
|
64 | $ mathjs --string --parenthesis=all
|
65 | > (1+1+1)
|
66 | (1 + 1) + 1
|
67 | ```
|
68 |
|
69 | # Command line debugging (REPL)
|
70 |
|
71 | For debugging purposes, `bin/repl.js` provides a REPL (Read Evaluate Print Loop)
|
72 | for interactive testing of mathjs without having to build new build files after every
|
73 | little change to the source files. You can either start it directly (`./bin/repl.js`) or
|
74 | via node (`node bin/repl.js`).
|
75 |
|
76 | You can exit using either [ctrl]-[C] or [ctrl]-[D].
|
77 |
|
78 | ```bash
|
79 | $ ./bin/repl.js
|
80 | > math.parse('1+1')
|
81 | { op: '+',
|
82 | fn: 'add',
|
83 | args:
|
84 | [ { value: '1', valueType: 'number' },
|
85 | { value: '1', valueType: 'number' } ] }
|
86 | >
|
87 | ```
|